def run(data_dir): detector = Detector() with open('data.tsv', 'r') as fp: r = csv.reader(fp, delimiter='\t') next(r) for row in r: photo_url = row[3] basename = os.path.basename(photo_url) path0 = f'{ord(basename[0]):02x}' path1 = f'{ord(basename[1]):02x}' path2 = f'{ord(basename[2]):02x}' filepath = os.path.join(data_dir, 'images', path0, path1, path2, basename) print(f'processing {filepath} ...') try: result = detector.detect(cv2.imread(filepath)) if result is None: print('detection failed.') continue outdir = os.path.join(data_dir, 'results', path0, path1, path2) os.makedirs(outdir, exist_ok=True) img = result['image'] del result['image'] result['meta'] = { 'face_id': row[0], 'photo_id': row[1], 'source_url': row[2], 'photo_url': row[3], 'posted_at': row[4], 'label_id': row[5], 'label_name': row[6], } name = os.path.splitext(basename)[0] cv2.imwrite(os.path.join(outdir, f'{name}.png'), img) with open(os.path.join(outdir, f'{name}.json'), 'w') as fp: json.dump(result, fp, ensure_ascii=False) except: print(f'error!!: {filepath}') time.sleep(1)
class CalcGridLayout(GridLayout): filePath = StringProperty('') weights = r'weights\best-100.pt' grafic_list = [] label_list = [] weights_description = [ 'Red entrenada 100 ciclos con dataset SVHN', 'Red entrenada 1000 ciclos con dataset SVHN', 'Red entrenada 1000 ciclos con dataset UNO Cards', "Red entrenada 1000 ciclos con dataset SVHN con datos incrementados" ] def __init__(self, **kwargs): super(CalcGridLayout, self).__init__(**kwargs) Window.bind(on_dropfile=self._on_file_drop) self.detector = Detector(self.weights, self.filePath) self.ids.model_label.text = self.weights_description[0] def reduced_image(self): pass def _on_file_drop(self, window, file_path): self.reset_canvas() self.filePath = file_path.decode("utf-8") self.ids.img.source = self.filePath self.ids.img.reload() self.ids.message_label.text = ' ' self.ids.start.visible = True self.detector.change_source(self.filePath) def change_weight(self, value): self.ids.model_label.text = self.weights_description[value] if value == 0: self.weights = 'weights/best-100.pt' if value == 1: self.weights = 'weights/best-1000.pt' if value == 2: self.weights = 'weights/uno.pt' if value == 3: self.weights = 'weights/best-fused.pt' self.detector.change_weights(self.weights) def start_detection(self): self.reset_canvas() self.coordinates = self.detector.detect() self.draw_rectangle(self.coordinates) def reset_canvas(self): for item in self.grafic_list: self.canvas.remove(item) for item in self.label_list: self.ids.float_canvas.remove_widget(item) for item in self.canvas.children: if 'Line' in repr(item): self.canvas.remove(item) def draw_rectangle(self, coordinates): print(coordinates) full_size_x = self.ids.img.size[0] full_size_y = self.ids.img.size[1] center = (full_size_x / 2, full_size_y / 2) image = Image.open(self.filePath) image_width = int(image.size[0]) image_height = int(image.size[1]) for coordinate in coordinates: rectangle_size_x = coordinate['bottom_right'][0] - coordinate[ 'top_left'][0] rectangle_size_y = coordinate['bottom_right'][1] - coordinate[ 'top_left'][1] relative_x_position = (center[0] - image_width / 2 + coordinate['top_left'][0]) relative_y_position = (center[1] + image_height / 2 - coordinate['top_left'][1] - rectangle_size_y) with self.canvas: self.obj = InstructionGroup() self.obj.add(Color(0, 1, 0, 0.3, mode="rgba")) self.obj.add( Line(rectangle=(relative_x_position, relative_y_position, rectangle_size_x, rectangle_size_y), width=3)) self.grafic_list.append(self.obj) label_position_x = relative_x_position - center[ 0] + rectangle_size_x / 2 label_position_y = relative_y_position - center[ 1] + rectangle_size_y * 1.2 label = Label(text=coordinate['label'], pos_hint={ 'x': label_position_x / full_size_x, 'y': label_position_y / full_size_y }, size=(self.ids.float_canvas.size[0], self.ids.float_canvas.size[1]), size_hint=(None, None), bold=True, outline_color=[1, 0, 0, 0.5], outline_width=2) self.ids.float_canvas.add_widget(label) self.label_list.append(label)
videoframe = cv2.VideoCapture(videoname) framenr = 0 if not videoframe.isOpened(): print("Error opening video stream or file") while videoframe.isOpened(): ret, img = videoframe.read() if not ret: # reached end of video break start = time.time() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) detections = detector.detect(img) det_list, crop_list = detector.get_detections(img, detections) det_ids = reid.evaluate_query(crop_list) detector.save_pic_with_detections(img, detections, det_ids, title=f"pic-{framenr:04}") print(f"Elaborating frame {framenr} fps: {1 / (time.time() - start):.3}") framenr += 1 print("Processing finished, saving video") videoframe.release() cv2.destroyAllWindows() save_video(work_dir, output_dir, filename=opt.outname) print(f"Video saved as {os.path.join(output_dir, opt.outname)}")
def worker(): provider = ServerAudioProvider(queue) detector = Detector(provider) detector.detect()
def annotate(cap,text_file_path): global frameNum frameNum = 0 cap.read() _, first_frame = cap.read() mask = toolMaskDrawer.main('',image=first_frame) #need to massively change below lines parameters = importlib.import_module("parameters.will1") #parameters.red_range,parameters.white_range = automation.findColorRanges(first_frame,debug=True) global detector detector = Detector(mask,parameters) global state state = State(parameters) #global current_positions #current_positions = {i:(-1,-1) for i in range(10)} global ball_infos ball_infos = [BallInfo(state.getBall(i)) for i in range(10)] global text_file initial_frameNum = 0 if cont: read_file = open(text_file_path,"r") remember_line = '' d = {} while(True): line = read_file.readline() if line == '': break remember_line = line _, d = read_annotation.processLine(line) for k,v in d.items(): print k,v ball_infos[k].ball.position = v ball_infos[k].position = v initial_frameNum = int(remember_line.split(':')[0]) text_file = open(text_file_path,"a") else: text_file = open(text_file_path,"w") circles = detector.detect(first_frame) if not cont: for color in [Color.RED,Color.WHITE]: colored_circles = sorted(circles[color], key=lambda c: c.x)#copied code from tracker v3 balls = state.red_balls if color == Color.RED else state.white_balls for i,ball in enumerate(balls): if i >= len(colored_circles): break ball.position = colored_circles[i].center() while(initial_frameNum!=frameNum): ret,frame = cap.read() frameNum+=1 while(cap.isOpened()): global frame, frameNum ret, frame = cap.read() frameNum += 1 redraw() if not handleInput(): break write() cv2.destroyAllWindows()
def main(): yolo = Detector() # Definition of the parameters max_cosine_distance = 0.3 nn_budget = None nms_max_overlap = 1.0 # deep_sort model_filename = 'model_data/mars-small128.pb' encoder = gdet.create_box_encoder(model_filename, batch_size=1) metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget) tracker = Tracker(metric) writeVideo_flag = True video_capture = cv2.VideoCapture("town.avi") if writeVideo_flag: # Define the codec and create VideoWriter object w = int(video_capture.get(3)) h = int(video_capture.get(4)) fourcc = cv2.VideoWriter_fourcc(*'MJPG') out = cv2.VideoWriter('output.avi', fourcc, 15, (w, h)) list_file = open('detection.txt', 'w') frame_index = -1 fps = 0.0 maxframe = 1 nframe = 0 while True: ret, frame = video_capture.read() # frame shape 640*480*3 if ret != True: break t1 = time.time() detections = [] # image = Image.fromarray(frame) nframe += 1 if (nframe >= maxframe): boxs, obj = yolo.detect(frame) print(len(boxs)) # print("box_num",len(boxs)) features = encoder(frame, boxs) # score to 1.0 here). detections = [ Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features) ] # Run non-maxima suppression. boxes = np.array([d.tlwh for d in detections]) scores = np.array([d.confidence for d in detections]) indices = preprocessing.non_max_suppression( boxes, nms_max_overlap, scores) detections = [detections[i] for i in indices] tracker.predict() tracker.update(detections) # Call the tracker nframe = 0 for track in tracker.tracks: if not track.is_confirmed() or track.time_since_update > 1: continue bbox = track.to_tlbr() # # s=recognize(img) # try: # if(len(s)>len(track.track_lpn)): # track.track_lpn=s # except Exception as e: # print(e) cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 255, 255), 2) cv2.putText(frame, str(track.track_id), (int(bbox[0]), int(bbox[1])), 0, 5e-3 * 200, (0, 255, 0), 2) # for det in detections: # bbox = det.to_tlbr() # cv2.rectangle(frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,0,0), 2) cv2.imshow('', frame) if writeVideo_flag: # save a frame out.write(frame) frame_index = frame_index + 1 list_file.write(str(frame_index) + ' ') fps = (fps + (1. / (time.time() - t1))) / 2 print("fps= %f" % (fps)) # Press Q to stop! if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() if writeVideo_flag: out.release() list_file.close() cv2.destroyAllWindows()
#infFile = "/home/leo/qj/object_detection/data/socr/inference_frcnn/frozen_inference_graph.pb" #mapFile = "/home/leo/qj/object_detection/data/socr/pascal_label_map.pbtxt" infFile = "/home/leo/qj/object_detection/data/socr/inference_ssd/frozen_inference_graph.pb" mapFile = "/home/leo/qj/object_detection/data/socr/pascal_label_map.pbtxt" #infFile = "/home/leo/qj/object_detection/data/Index/inference_ssd/frozen_inference_graph.pb" #mapFile = "/home/leo/qj/object_detection/data/Index/pascal_label_map.pbtxt" imFiles = [ "/home/leo/qj/object_detection/data/origin/socr_20180410/JPEGImages/im0505.jpg", "/home/leo/qj/object_detection/data/origin/socr_20180410/JPEGImages/im0506.jpg", "/home/leo/qj/object_detection/data/origin/socr_20180410/JPEGImages/im0507.jpg", "/home/leo/qj/object_detection/data/origin/socr_20180410/JPEGImages/im0508.jpg" ] detector = Detector('socr', infFile, mapFile) for imFile in imFiles: img_np = np.asarray(Image.open(imFile)) img_np.setflags(write=1) s = time.time() (image_np, tag_boxes, tag_scores, tag_classes, tag_num) = detector.detect(img_np, visualize=False) e = time.time() print 'detecting:', imFile print 'detect time is:', (e - s) print 'tag_num:', tag_num for c in tag_classes: if tag_scores[c] >= 0.8: print 'tag:(%s,%f)', c, tag_scores[c]