Esempio n. 1
0
 def run(self, image_name, get_label=False, do_detection=1):
     """detection and extraction with max score box"""
     ### for web demo
     #caffe.set_mode_gpu()
     #print "do_detection: ",do_detection
     if do_detection:
         t1 = Timer()
         t1.tic()
         image = self.detect(image_name)
         t1.toc('Detect time: ')
         #print "Detection has done"
     else:
         image = cv2.imread(image_name)
         #image = imresize(im, 300)
     t2 = Timer()
     t2.tic()
     image = pad(image,size=224)
     #image = pad(image)
     features = extraction.forward(self.net_e, image, self.transformer)
     r = np.squeeze(features['pool5/7x7_s1'].data[0])
     #features2 = extraction.forward(self.net_e2, image, self.transformer2)
     #r2 = np.squeeze(features2['pool5/7x7_s1'].data[0])
     #r = r2
     #r = np.hstack((r, r2)).copy()
     t2.toc('extract time: ')
     #start = time.time()
     if self.pca is not None:
         r = self.pca.transform(r)[0,:]
         #print 'pca time: ', time.time() - start
     r = r/norm(r)
     if get_label:
         label = np.squeeze(features['prob'].data[0].copy())
         return r, label
     return r
Esempio n. 2
0
def detect_video(detector, device, args):
    """Demo for detecting video."""
    timer = Timer()
    input_video = cv.VideoCapture(args.video)
    frame_width = int(input_video.get(cv.CAP_PROP_FRAME_WIDTH))
    frame_height = int(input_video.get(cv.CAP_PROP_FRAME_HEIGHT))
    output_video = cv.VideoWriter()
    if args.save:
        output_video.open('record.avi', cv.VideoWriter_fourcc(*'XVID'),
                          input_video.get(cv.CAP_PROP_FPS),
                          (frame_width, frame_height), True)
    frame = np.empty([frame_height, frame_width, 3], dtype=np.uint8)
    while input_video.read(frame)[0]:
        timer.tic()
        frame = undistort(frame)
        pred_points = detect_marking_points(
            detector, frame, args.thresh, device)
        slots = None
        if pred_points and args.inference_slot:
            marking_points = list(list(zip(*pred_points))[1])
            slots = inference_slots(marking_points)
        timer.toc()
        plot_points(frame, pred_points)
        plot_slots(frame, pred_points, slots)
        cv.imshow('demo', frame)
        cv.waitKey(1)
        if args.save:
            output_video.write(frame)
    print("Average time: ", timer.calc_average_time(), "s.")
    input_video.release()
    output_video.release()
Esempio n. 3
0
 def search(self, image_path, do_detection=1, k=10): 
     #queryImage = cv2.imread(image_path)
     t1 = Timer()
     t1.tic()
     #queryFeatures = descriptor.get_descriptor(image_path, multi_box=False)
     queryFeatures = descriptor.get_descriptor(image_path)
     
     t1.toc('Feature Extraction time: ')
     t2 = Timer()
     t2.tic()
     #p = Profile()
     #results = p.runcall(self.searcher.search, queryFeatures)
     #p.print_stats()
     results, dists, ind = self.searcher.search(queryFeatures,k=5*k)
     #self.reranking(queryFeatures, results, dists, ind, 0.6)
     #self.queryExpansion2(results, dists, ind)
     #self.queryExpansion(queryFeatures, results, dists, ind, top=3)
     t2.toc('Knn search time: ')
     result = []
     # origine image
     #result.append(image_path)
     dist = []
     for j,imageName in enumerate(results):
         if imageName not in result:
             result.append(imageName)
             dist.append(dists[j])
     #print result[:k]
     return result[:k],dist[:k]
Esempio n. 4
0
 def search(self, image_path, do_detection=1, k=50): 
     #queryImage = cv2.imread(image_path)
     t1 = Timer()
     t1.tic()
     #queryFeatures = descriptor.get_descriptor(image_path, multi_box=False)
     queryFeatures, label = descriptor.get_descriptor(image_path,
                                                      multi_box=False,
                                                      get_label=True,
                                                      do_detection=do_detection)
     flag = []
     #flag = [] # if do, we donot use class to filter result       
     t1.toc('Feature Extraction time: ')
     t2 = Timer()
     t2.tic()
     #p = Profile()
     #results = p.runcall(self.searcher.search, queryFeatures)
     #p.print_stats()
     results, dists = self.searcher.search(queryFeatures)
     print dists
     t2.toc('Knn search time: ')
     result = []
     # origine image
     #result.append(image_path)
     if len(flag) != 0:
         for j in xrange(0, k):
             imageName = results[j]
             if imageName not in result:    
                 #Juge class error but image similarity is high
                 if dists[j] < 0.05:
                     result.append(imageName)
                     continue
                 #if dists[j] > 0.2:
                 #    break
                 #judge wether image belongs to the class
                 image_path = imageName.split('/')
                 image_dir = image_path[0]+'/'+image_path[1]+'/'+image_path[2]
                 #print image_dir
                 if image_dir in flag:
                     result.append(imageName)
                 #else:
                 #    result.append(imageName)
     print 'total result', len(result)
     if len(result)<3:
         # if result about class is less than 5, we do search in all datasets
         #print 'total result', len(result)
         k = 30
         result = []
         for j in xrange(0, k):
             imageName = results[j]
             if imageName not in result:
                 #if dists[j] > 0.2:
                 #    break
                 result.append(imageName)
     
     return result 
Esempio n. 5
0
def detect_image(detector, device, args):
    """Demo for detecting images."""
    timer = Timer()
    while True:
        image_file = input('Enter image file path: ')
        image = cv.imread(image_file)
        timer.tic()
        pred_points = detect_marking_points(detector, image, args.thresh,
                                            device)
        slots = None
        if pred_points and args.inference_slot:
            marking_points = list(list(zip(*pred_points))[1])
            slots = inference_slots(marking_points)
        timer.toc()
        plot_points(image, pred_points)
        plot_slots(image, pred_points, slots)
        cv.imshow('demo', image)
        cv.waitKey(1)
        if args.save:
            cv.imwrite('save.jpg', image, [int(cv.IMWRITE_JPEG_QUALITY), 100])
Esempio n. 6
0
 def search(self, image_path, do_detection=0, k=20): 
     t1 = Timer()
     t1.tic()
     #queryFeatures = descriptor.get_descriptor(image_path, multi_box=False)
     queryFeatures = descriptor.get_descriptor(image_path,do_detection=do_detection)
     
     t1.toc('Feature Extraction time: ')
     t2 = Timer()
     t2.tic()
     results, dists, ind = self.searcher.search(queryFeatures,k=k)
     #self.queryExpansion(results, dists, ind)
     #self.queryExpansion(results, dists, ind)
     t2.toc('Knn search time: ')
     result = []
     dist = []
     for j,imageName in enumerate(results):
         if imageName not in result:
             result.append(imageName)
             dist.append(dists[j])
     return result[:k],dist[:k]
Esempio n. 7
0
    def detect(self, image_name, multi_box=False, classes=CLASSES[1:]):
        """detect clothes in image"""
        t1 = Timer()
        t1.tic()
        im = cv2.imread(image_name)
        #im = imresize(im)
        t1.toc('read image time: ')
        t2 = Timer()
        t2.tic()
        scores, boxes = im_detect(self.net_d, im)
        t2.toc('faster-rcnn time: ')
        CONF_THRESH = 0.8
        NMS_THRESH = 0.3
        #dets = np.ones((5))
        max_score = 0
        if not multi_box:
            f = open('bbox.txt','w')
            for cls in classes:
                cls_ind = CLASSES.index(cls)
                cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
                cls_scores = scores[:, cls_ind]
                box = np.hstack((cls_boxes, cls_scores[:,
                                                       np.newaxis])).astype(np.float32)
                keep = nms(box, NMS_THRESH)
                cls_boxes = box[keep, :4]
                cls_scores = box[keep, 4]
                score = max(cls_scores)
                if score > CONF_THRESH:
                    ind = cls_scores.argmax()
                    max_score = score
                    det = cls_boxes[ind,:]
                    f.write(cls)
                    f.write('\n')
                    for d in det:
                        f.write(str(d))
                        f.write(' ')
                    #print cls
                    break
            f.close()
            if max_score == 0:
		        return im
            else:
                bbox = det.astype(np.float32)
                #imshow(crop(im,bbox))
	        return crop(im,bbox)

        else:
            multi_im = []
            dets = []
            for cls in classes:
                cls_ind = CLASSES.index(cls)
                cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
                cls_scores = scores[:, cls_ind]
                box = np.hstack((cls_boxes, cls_scores[:,
                                                       np.newaxis])).astype(np.float32)
                keep = nms(box, NMS_THRESH)
                cls_boxes = box[keep, :4]
                cls_scores = box[keep, 4]
                score = max(cls_scores)
                if score > CONF_THRESH:
                    max_score = score
                    ind = cls_scores.argmax()
                    det = cls_boxes[ind,:]
                    if cls == '7' and (det[3]-det[1])<im.shape[0]/4 and det[3]>im.shape[0]-30:
                        break
                    if cls == '1' or cls == '4':
                        dets.append(det.copy())
                        break
                    dets.append(det.copy())
            if max_score == 0:
                multi_im.append(im)
                return multi_im
            else:
                for det in dets:
                    bbox = det.astype(np.float32)
                    multi_im.append(crop(im,bbox))
	        return multi_im