def main(): parser = ap.ArgumentParser( description= "Arguments to use in data collection and Arduino communication.") parser.add_argument("port", help="The serial port file to use.") args = parser.parse_args() serial_port = args.port if not os.path.exists(serial_port): print("Error: file '{}' does not exist or is not a file.".format( serial_port)) exit(1) # William's test driver code, but with the serial port as a command line arg. # ctl = ac(serial_port) # need this delay for arduino initialization time.sleep(3) print('Starting detection...') detector = Detector(serial_port) while True: try: print('writing: ' + str(detector.sample())) except Exception as e: print(e) print('Breaking') detector.close() break
def video_img(): detector = Detector() vc=cv2.VideoCapture(r'F:\video\mv.mp4') c=1 if vc.isOpened(): rval,frame=vc.read() else: rval=False while rval: rval,frame=vc.read() im = Image.fromarray(cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)) boxes = detector.detect(im) for box in boxes: x1 = int(box[0]) y1 = int(box[1]) x2 = int(box[2]) y2 = int(box[3]) _y2 = int(y2 * 0.8+y1*0.2) cv2.rectangle(frame,(x1, y1), (x2, _y2), (0,225,0),2) cv2.imshow('img',frame) # cv2.imwrite(r'F:\video\img\img'+str(c)+'.jpg',frame) c=c+1 cv2.waitKey(1) vc.release()
def run(datafile, data_dir): def download(url): try: with urlopen(url) as response: if response.status == 200: data = np.asarray(bytearray(response.read()), dtype=np.uint8) return cv2.imdecode(data, cv2.IMREAD_COLOR) except HTTPError as e: print(f'{url}: {e}') except (ConnectionError, OSError) as e: print(f'{url}: {e}') time.sleep(0.1) except Exception as e: print(f'{url}: {e}') time.sleep(0.5) return None detector = Detector() with open(datafile, 'r') as fp: r = csv.reader(fp, delimiter='\t') for row in r: photo_url = row[1] print(photo_url) img = download(photo_url) if img is None: continue result = detector.detect(img) if result is None: continue basename = os.path.basename(photo_url) path0 = f'{ord(basename[0]):02x}' path1 = f'{ord(basename[1]):02x}' path2 = f'{ord(basename[2]):02x}' outdir = os.path.join(data_dir, path0, path1, path2) os.makedirs(outdir, exist_ok=True) faceimg = result['image'] del result['image'] created_at = datetime.fromtimestamp( time.mktime(time.strptime(row[3], '%a %b %d %H:%M:%S +0000 %Y'))) result['meta'] = { 'photo_id': row[0], 'photo_url': row[1], 'source_url': row[2], 'published_at': created_at.isoformat(), 'label_id': row[4], 'label_name': row[5], } name = os.path.splitext(basename)[0] cv2.imwrite(os.path.join(outdir, f'{name}.jpg'), faceimg, [cv2.IMWRITE_JPEG_QUALITY, 100]) with open(os.path.join(outdir, f'{name}.json'), 'w') as fp: json.dump(result, fp, ensure_ascii=False)
def main_func(path): """ Detect 3rd-party libraries in app. The detection result is printed. :param path: The path of target app. :return: None. """ print "--Decoding--" detector = Detector() decoded_path = detector.get_smali(path) detector.get_hash(decoded_path)
def __init__(self, opt): super(mywindow, self).__init__() self.setupUi(self) self.setFixedSize(self.width(), self.height()) self.opt = opt self.detector = Detector(cfg=opt.cfg, data_cfg=opt.data_cfg, weights=opt.weights, img_size=opt.img_size, conf_thres=opt.conf_thres, nms_thres=opt.nms_thres)
def __init__(self, minutes): queue = Queue(maxsize=100) self.queue = queue self.recorder = Recorder() self.minutes = minutes self.consoleAudioHandler = ConsoleAudioHandler(self.queue, config["tmpPath"]) t = Thread(target=self.worker) t.start() provider = ConsoleAudioProvider(self.queue) detector = Detector(provider) detector.detect()
class TrafficLightDetector: def __init__(self): self.detector = Detector() def detect(self, image): boxes = self.detector.detect(image) traffic_light_boxes = self.filter(boxes, "traffic light") box_image = self.detector.drawBoxes(image, traffic_light_boxes) return traffic_light_boxes, box_image def filter(self, boxes, category): filtered_boxes = [] for box in boxes: left, right, top, bot, mess, max_indx, confidence = box if mess == category: filtered_boxes.append(box) return filtered_boxes
class mywindow(QTabWidget, Ui_TabWidget): #这个窗口继承了用QtDesignner 绘制的窗口 def __init__(self, opt): super(mywindow, self).__init__() self.setupUi(self) self.setFixedSize(self.width(), self.height()) self.opt = opt self.detector = Detector(cfg=opt.cfg, data_cfg=opt.data_cfg, weights=opt.weights, img_size=opt.img_size, conf_thres=opt.conf_thres, nms_thres=opt.nms_thres) def videoprocessing(self): global videoName #在这里设置全局变量以便在线程中使用 videoName, videoType = QFileDialog.getOpenFileName( self, "打开视频", "", " *.MOV;;*.mp4;;*.avi;;All Files (*)") th = Thread(self) th.changePixmap.connect(self.setImage) th.start() def setImage(self, image): self.label.setPixmap(QPixmap.fromImage(image)) def imageprocessing(self): self.imgName, imgType = QFileDialog.getOpenFileName( self, "打开图片", "", #" *.jpg;;*.png;;*.jpeg;;*.bmp") " *.jpg;;*.png;;*.jpeg;;*.bmp;;All Files (*)") #利用qlabel显示图片 # print(str(self.imgName)) png = QtGui.QPixmap(self.imgName).scaled( self.label1.width(), self.label1.height()) #适应设计label时的大小 self.label1.setPixmap(png) def imageprocessing2(self): opt = self.opt with torch.no_grad(): start = time.time() savepath = self.detector.detect_one_image(self.imgName) # savepath = detect_one_image(opt.cfg, # opt.data_cfg, # opt.weights, # image_path=self.imgName, # output='output', # output folder # img_size=416, # conf_thres=0.5, # nms_thres=0.5) print('cost time is {}s'.format(round(time.time() - start, 2))) savepath = osp.abspath(savepath) #利用qlabel显示图片 print(str(savepath)) png = QtGui.QPixmap(savepath).scaled( self.label1.width(), self.label1.height()) #适应设计label时的大小 self.label1.setPixmap(png)
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)
def main(): args = parseopt() dump_flow(args.movie) flow, header = pick_flow(args.movie) detector = Detector(gpu=args.gpu) draw_box_flow_func = \ lambda movie, flow: draw_box_flow(movie, flow, detector) if args.nobbox: vis_flow(args.movie, flow) else: vis_flow(args.movie, flow, draw=draw_box_flow_func)
def track(video_path, use_gpu=False): video = cv2.VideoCapture(video_path) ret, frame = video.read() if ret: frame = cv2.resize(frame, (input_width, input_height)) if use_gpu: caffe.set_mode_gpu() tracker = Sort(max_age=10) detector = Detector() classes = detector.get_classes() while ret: frame_disp = np.copy(frame) bounding_boxes, counting = detector.infer(frame) class_counting = zip(classes, counting) for pair in class_counting: print('{:s} {:03d}'.format(*pair)) print('') if len(bounding_boxes) > 0: bounding_boxes = np.array(bounding_boxes, np.int32) # convert (x, y, w, h) to (x1, y1, x2, y2) bounding_boxes[:, 2:4] += bounding_boxes[:, 0:2] bounding_boxes[:, 2:4] -= 1 track_results = tracker.update(bounding_boxes) draw_tracking_results(track_results, frame_disp) cv2.imshow('tracking', frame_disp) key = cv2.waitKey(1) if key == 27: return ret, frame = video.read() if ret: frame = cv2.resize(frame, (input_width, input_height))
class Controller: def __init__(self): self.detector = Detector() def getScreenShot(self): os.system("adb shell screencap -p /sdcard/tmp.png") os.system("adb pull /sdcard/tmp.png") img = cv2.imread("tmp.png") return img def tap(self): img = self.getScreenShot() h, w, t = img.shape print(img.shape) positons = self.detector.detect(img) print(positons) hRate = 1920 / 2243 wRate = 1080 / 1079 for x, y in positons: x = (x + 20) / 800 * h y = (y + 20) / 400 * w print(x, y) os.system("adb shell input tap " + str(int(y)) + " " + str(int(x))) def searchAvailableAndTap(self): img = self.getScreenShot() for i in range(img.shape[0]): if img[i, -1][0] == 109: os.system("adb shell input tap " + str(100) + " " + str(int(i))) time.sleep(1) self.tap() time.sleep(1) os.system("adb shell input keyevent 4") img = self.getScreenShot() i += 100 def swipe(self): os.system("adb shell input swipe 0 1000 0 0") def run(self): self.showMoreFriends() time.sleep(1) for i in range(10): self.searchAvailableAndTap() self.swipe() os.system("adb shell input keyevent 4") def showMoreFriends(self): img = self.getScreenShot() os.system("adb shell input tap " + str(100) + " " + str(int(img.shape[0] - 20)))
def __init__(self): handlers = [ (r"/config", ConfigHandler), (r"/voice", VoiceHandler), (r"/detect", HttpDetectHandler), ] settings = dict( debug=options.dev, static_path=os.path.join( os.path.dirname(os.path.abspath(__file__)), "static"), template_path=os.path.join( os.path.dirname(os.path.abspath(__file__)), "templates"), ) super(Application, self).__init__(handlers, **settings) self.data = None self.color_map = collections.defaultdict(lambda: (random.randint( 0, 255), random.randint(0, 255), random.randint(0, 255))) for i in range(1, 21): self.color_map[str(i)] = [ random.randint(0, 255), random.randint(0, 255), random.randint(0, 255) ] if options.model.find('det300') >= 0: self.model_det300 = Detector('conf/retina_net.conf') if options.model.find('det600') >= 0: self.model_det600 = Detector('conf/retina_net_600.conf') if options.model.find('mask600') >= 0: self.model_mask600 = MaskRetinaNet('conf/mask_600.conf') if options.model.find('seg512') >= 0: self.model_seg512 = Segmentor('conf/cloth_320.conf', rotate=False) self.model_det300 = Detector('conf/retina_net.conf') self.model_fat = FatDetector() self.model_multi_task = MultiTaskDetector() logging.info("initialize done")
def result_video(): if request.method == 'POST': if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] if file.filename == '': flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) filename = os.path.join(UPLOAD_FOLDER, filename) file.save(filename) model = Detector(filename, 1) model.run_detector() model.generate_plots() model.write_video() return render_template('result_video.html') #return redirect(url_for('uploaded_file',filename=filename)) return render_template('upload_video.html')
if loc is None: loc = Locator(frame) # loc.locate_tube() # loc.locate_window() # loc.locate_scale() loc.tube = [(0, 271, 33, 76), (0, 271, 78, 115)] loc.window = [[(47, 80, 43, 61), (82, 115, 42, 58), (117, 151, 39, 58), (153, 186, 41, 61), (188, 220, 43, 61)], [(2, 35, 88, 108), (35, 69, 85, 105), (72, 105, 84, 105), (108, 142, 84, 104), (145, 178, 84, 104), (179, 213, 87, 108), (214, 246, 89, 109)]] loc.scale = [[110, 199], [102, 199]] tube0 = Detector(loc.tube[0], loc.window[0], loc.scale[0], 210, 50) tube1 = Detector(loc.tube[1], loc.window[1], loc.scale[1], 160, 33) tube0.feed(frame) tube1.feed(frame) if not count % 20: tube0.update_backgrounds() tube1.update_backgrounds() level = int(tube0.cur_level) cv2.line(frame, (45, level), (60, level), (0, 0, 255), 1) level = int(tube1.cur_level) cv2.line(frame, (90, level), (105, level), (0, 0, 255), 1) text0 = 'level0: ' + str(round(tube0.level_scale)) text1 = 'level1: ' + str(round(tube1.level_scale)) cv2.putText(frame, text0, (5, 13), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.6, (0, 0, 255), 1)
def __init__(self): # Create the olympe.Drone object from its IP address self.drone = olympe.Drone(DRONE_IP) # subscribe to flight listener listener = FlightListener(self.drone) listener.subscribe() self.last_frame = np.zeros((1, 1, 3), np.uint8) self.frame_queue = queue.Queue() self.flush_queue_lock = threading.Lock() self.detector = Detector() self.keypoints_image = np.zeros((1, 1, 3), np.uint8) self.keypoints = deque(maxlen=5) self.faces = deque(maxlen=10) self.f = open("distances.csv", "w") self.face_distances = deque(maxlen=10) self.image_width = 1280 self.image_height = 720 self.half_face_detection_size = 150 self.poses_model = load("models/posesmodel.joblib") self.pose_predictions = deque(maxlen=5) self.pause_finding_condition = threading.Condition(threading.Lock()) self.pause_finding_condition.acquire() self.pause_finding = True self.person_thread = threading.Thread(target=self.fly_to_person) self.person_thread.start() # flight parameters in meter self.flight_height = 0.0 self.max_height = 1.0 self.min_dist = 1.5 # keypoint map self.nose = 0 self.left_eye = 1 self.right_eye = 2 self.left_ear = 3 self.right_ear = 4 self.left_shoulder = 5 self.right_shoulder = 6 self.left_elbow = 7 self.right_elbow = 8 self.left_wrist = 9 self.right_wrist = 10 self.left_hip = 11 self.right_hip = 12 self.left_knee = 13 self.right_knee = 14 self.left_ankle = 15 self.right_ankle = 16 # person distance self.eye_dist = 0.0 # save images self.save_image = False self.image_counter = 243 self.pose_file = open("poses.csv", "w") super().__init__() super().start()
parser.add_argument("--reidpath", type=str, default='reidLib', help="outputdir") opt = parser.parse_args() videoname = os.path.join(opt.indir,opt.videoin) output_dir = opt.outdir work_dir = opt.workdir reid_dir = opt.reiddir if opt.reiddir != "" else None make_folder(output_dir) clean_folder(work_dir) clean_folder(reid_dir) sys.path.append(opt.yolopath) from detect import Detector detector = Detector(conf_thres=opt.detconf, yolo_path=opt.yolopath, resize_size=opt.imgsize, output_dir=work_dir) from reid import Reid reid = Reid(save_path=reid_dir, threshold=opt.threshold, verbose=False) 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
def __init__(self): self.detector = Detector()
class MainClass(threading.Thread): def __init__(self): # Create the olympe.Drone object from its IP address self.drone = olympe.Drone(DRONE_IP) # subscribe to flight listener listener = FlightListener(self.drone) listener.subscribe() self.last_frame = np.zeros((1, 1, 3), np.uint8) self.frame_queue = queue.Queue() self.flush_queue_lock = threading.Lock() self.detector = Detector() self.keypoints_image = np.zeros((1, 1, 3), np.uint8) self.keypoints = deque(maxlen=5) self.faces = deque(maxlen=10) self.f = open("distances.csv", "w") self.face_distances = deque(maxlen=10) self.image_width = 1280 self.image_height = 720 self.half_face_detection_size = 150 self.poses_model = load("models/posesmodel.joblib") self.pose_predictions = deque(maxlen=5) self.pause_finding_condition = threading.Condition(threading.Lock()) self.pause_finding_condition.acquire() self.pause_finding = True self.person_thread = threading.Thread(target=self.fly_to_person) self.person_thread.start() # flight parameters in meter self.flight_height = 0.0 self.max_height = 1.0 self.min_dist = 1.5 # keypoint map self.nose = 0 self.left_eye = 1 self.right_eye = 2 self.left_ear = 3 self.right_ear = 4 self.left_shoulder = 5 self.right_shoulder = 6 self.left_elbow = 7 self.right_elbow = 8 self.left_wrist = 9 self.right_wrist = 10 self.left_hip = 11 self.right_hip = 12 self.left_knee = 13 self.right_knee = 14 self.left_ankle = 15 self.right_ankle = 16 # person distance self.eye_dist = 0.0 # save images self.save_image = False self.image_counter = 243 self.pose_file = open("poses.csv", "w") super().__init__() super().start() def start(self): self.drone.connect() # Setup your callback functions to do some live video processing self.drone.set_streaming_callbacks( raw_cb=self.yuv_frame_cb, start_cb=self.start_cb, end_cb=self.end_cb, flush_raw_cb=self.flush_cb, ) # Start video streaming self.drone.start_video_streaming() # set maximum speeds print("rotation", self.drone(MaxRotationSpeed(1)).wait().success()) print("vertical", self.drone(MaxVerticalSpeed(0.1)).wait().success()) print("tilt", self.drone(MaxTilt(5)).wait().success()) def stop(self): # Properly stop the video stream and disconnect self.drone.stop_video_streaming() self.drone.disconnect() def yuv_frame_cb(self, yuv_frame): """ This function will be called by Olympe for each decoded YUV frame. :type yuv_frame: olympe.VideoFrame """ yuv_frame.ref() self.frame_queue.put_nowait(yuv_frame) def flush_cb(self): with self.flush_queue_lock: while not self.frame_queue.empty(): self.frame_queue.get_nowait().unref() return True def start_cb(self): pass def end_cb(self): pass def show_yuv_frame(self, window_name, yuv_frame): # the VideoFrame.info() dictionary contains some useful information # such as the video resolution info = yuv_frame.info() height, width = info["yuv"]["height"], info["yuv"]["width"] # yuv_frame.vmeta() returns a dictionary that contains additional # metadata from the drone (GPS coordinates, battery percentage, ...) # convert pdraw YUV flag to OpenCV YUV flag cv2_cvt_color_flag = { olympe.PDRAW_YUV_FORMAT_I420: cv2.COLOR_YUV2BGR_I420, olympe.PDRAW_YUV_FORMAT_NV12: cv2.COLOR_YUV2BGR_NV12, }[info["yuv"]["format"]] # yuv_frame.as_ndarray() is a 2D numpy array with the proper "shape" # i.e (3 * height / 2, width) because it's a YUV I420 or NV12 frame # Use OpenCV to convert the yuv frame to RGB cv2frame = cv2.cvtColor(yuv_frame.as_ndarray(), cv2_cvt_color_flag) # show video stream cv2.imshow(window_name, cv2frame) cv2.moveWindow(window_name, 0, 500) # show other windows self.show_face_detection(cv2frame) self.show_keypoints() cv2.waitKey(1) def show_keypoints(self): if len(self.keypoints) > 2: # display eye distance cv2.putText( self.keypoints_image, 'Distance(eyes): ' + "{:.2f}".format(self.eye_dist) + "m", (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) # display nose height cv2.putText( self.keypoints_image, 'Nose: ' + "{:.2f}".format( get_point(np.average(self.keypoints, axis=0), self.nose)[1] / self.image_height), (0, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) cv2.imshow("keypoints", self.keypoints_image) cv2.moveWindow("keypoints", 500, 0) def show_face_detection(self, cv2frame): # get sub image img = self.get_face_detection_crop(cv2frame) # get face rectangle face, img = self.detector.detect_face(img) if face.size > 0: self.faces.append(face) width = face[2] - face[0] height = face[3] - face[1] # get distances for rectangle width and height width = get_distance_width(width) height = get_distance_height(height) # display distances cv2.putText(img, 'width: ' + "{:.2f}".format(width), (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) cv2.putText(img, 'height: ' + "{:.2f}".format(height), (0, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) cv2.putText( img, 'mean: ' + "{:.2f}".format(np.mean(np.array([width, height]))), (0, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) # append outlier free distance to log self.face_distances.append(self.get_face_distance()) elif len(self.faces) > 0: # remove from dequeue if no face was detected self.faces.popleft() # show detection cv2.imshow("face", img) cv2.moveWindow("face", 0, 0) # get 300*300 crop of frame based on nose location or from the middle def get_face_detection_crop(self, cv2frame): if len(self.keypoints) > 0: x, y = get_point( np.array(self.keypoints, dtype=object)[-1], self.nose) else: x = cv2frame.shape[1] / 2 y = cv2frame.shape[0] / 2 x = max(self.half_face_detection_size, x) y = max(self.half_face_detection_size, y) x = min(cv2frame.shape[1] - self.half_face_detection_size, x) y = min(cv2frame.shape[0] - self.half_face_detection_size, y) img = cv2frame[int(y - self.half_face_detection_size ):int(y + self.half_face_detection_size), int(x - self.half_face_detection_size ):int(x + self.half_face_detection_size)] return img def get_face_distance(self): if len(self.faces) > 2: try: faces = remove_outliers(np.array(self.faces, dtype=object)) face = np.mean(faces, axis=0) width = face[2] - face[0] height = face[3] - face[1] width = get_distance_width(width) height = get_distance_height(height) return np.mean(np.array([width, height])) except ZeroDivisionError: logging.error("ZeroDivisionError in get_face_distance()") logging.error(self.faces) return -1 def run(self): window_name = "videostream" cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) main_thread = next( filter(lambda t: t.name == "MainThread", threading.enumerate())) while main_thread.is_alive(): with self.flush_queue_lock: try: yuv_frame = self.frame_queue.get(timeout=0.01) except queue.Empty: continue try: # the VideoFrame.info() dictionary contains some useful information # such as the video resolution info = yuv_frame.info() height, width = info["yuv"]["height"], info["yuv"]["width"] # yuv_frame.vmeta() returns a dictionary that contains additional # metadata from the drone (GPS coordinates, battery percentage, ...) # convert pdraw YUV flag to OpenCV YUV flag cv2_cvt_color_flag = { olympe.PDRAW_YUV_FORMAT_I420: cv2.COLOR_YUV2BGR_I420, olympe.PDRAW_YUV_FORMAT_NV12: cv2.COLOR_YUV2BGR_NV12, }[info["yuv"]["format"]] # yuv_frame.as_ndarray() is a 2D numpy array with the proper "shape" # i.e (3 * height / 2, width) because it's a YUV I420 or NV12 frame # Use OpenCV to convert the yuv frame to RGB cv2frame = cv2.cvtColor(yuv_frame.as_ndarray(), cv2_cvt_color_flag) self.last_frame = cv2frame self.show_yuv_frame(window_name, yuv_frame) except Exception: # We have to continue popping frame from the queue even if # we fail to show one frame traceback.print_exc() finally: # Don't forget to unref the yuv frame. We don't want to # starve the video buffer pool yuv_frame.unref() cv2.destroyWindow(window_name) def command_window_thread(self, win): win.nodelay(True) key = "" win.clear() win.addstr("Detected key:") while 1: try: key = win.getkey() win.clear() win.addstr("Detected key:") win.addstr(str(key)) # disconnect drone if str(key) == "c": win.clear() win.addstr("c, stopping") self.f.close() self.pose_file.close() self.drone.disconnect() # takeoff if str(key) == "t": win.clear() win.addstr("takeoff") assert self.drone(TakeOff()).wait().success() win.addstr("completed") # land if str(key) == "l": win.clear() win.addstr("landing") assert self.drone(Landing()).wait().success() # turn left if str(key) == "q": win.clear() win.addstr("turning left") assert self.drone( moveBy(0, 0, 0, -math.pi / 4) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() win.addstr("completed") # turn right if str(key) == "e": win.clear() win.addstr("turning right") assert self.drone( moveBy(0, 0, 0, math.pi / 4) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() win.addstr("completed") # move front if str(key) == "w": win.clear() win.addstr("front") assert self.drone( moveBy(0.2, 0, 0, 0) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() win.addstr("completed") # move back if str(key) == "s": win.clear() win.addstr("back") assert self.drone( moveBy(-0.2, 0, 0, 0) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() win.addstr("completed") # move up if str(key) == "r": win.clear() win.addstr("up") assert self.drone( moveBy(0, 0, -0.15, 0) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() win.addstr("completed") # move down if str(key) == "f": win.clear() win.addstr("down") assert self.drone( moveBy(0, 0, 0.15, 0) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() win.addstr("completed") # move left if str(key) == "a": win.clear() win.addstr("left") assert self.drone( moveBy(0, -0.2, 0, 0) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() win.addstr("completed") # move right if str(key) == "d": win.clear() win.addstr("right") assert self.drone( moveBy(0, 0.2, 0, 0) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() win.addstr("completed") # start person detection thread if str(key) == "p": win.clear() pause = self.check_for_pause() if pause: win.addstr("cannot start because of stop gesture") else: win.addstr("start detecting") self.pause_finding = False self.pause_finding_condition.notify() self.pause_finding_condition.release() # pause person detecting thread if str(key) == "o": win.clear() win.addstr("stop detecting") self.pause_finding = True self.pause_finding_condition.acquire() # self.person_thread.stop = True # win.addstr("joined") # measure distances if str(key) == "x": win.clear() win.addstr("distances:") arr = np.array(self.keypoints, dtype=object) string = "" for i in range(arr.shape[0]): string += "{:.6f};".format( get_point_distance(arr[i], self.left_eye, self.right_eye)) win.addstr(string) self.f.write(string + "\n") # measure faces if str(key) == "y": win.clear() win.addstr("distances:") arr = np.array(self.faces, dtype=object) width = "" height = "" for i in range(arr.shape[0]): width += str(arr[i][2] - arr[i][0]) + ";" height += str(arr[i][3] - arr[i][1]) + ";" win.addstr(width + height) self.f.write(width + "\n") self.f.write(height + "\n") # log user gesture if str(key) == "g": win.clear() win.addstr("gesture made") global event_time event_time = time.time() logging.info("stop gesture by user;{:.3f};{:.3f}".format( self.get_face_distance(), time.time())) # log face distances if str(key) == "k": win.clear() win.addstr("distances logged") string = "" arr = np.array(self.face_distances, dtype=object) win.addstr(str(len(arr))) for i in range(len(arr)): string += "{:.2f}".format(arr[i]) + ";" logging.info("distances;{}".format(string)) win.addstr(string) except Exception as e: # No input pass def fly_to_person(self): t = threading.currentThread() while not getattr(t, "stop", False): with self.pause_finding_condition: # wait if thread is paused while self.pause_finding: self.pause_finding_condition.wait() arr = np.array(self.keypoints, dtype=object) if len(arr) > 2: pose_predictions = np.array(self.pose_predictions, dtype=object) if pose_predictions[-1] > 1: logging.info( "stop gesture {} detected;{:.3f};{:.3f}".format( pose_predictions[-1], self.get_face_distance(), time.time() - event_time)) # check if multiple stop gestures were detected pause = self.check_for_pause() if pause: logging.info( f"stopping completely gesture {pose_predictions[-1]};{self.get_face_distance()};{time.time() - event_time}" ) # land drone assert self.drone(Landing()).wait().success() self.pause_finding = True self.pause_finding_condition.acquire() time.sleep(0.2) continue distance = self.get_face_distance() xn, yn = get_point(np.average(arr[-2:], axis=0), self.nose) # calculate angle of nose angle = (xn / self.image_width - 0.5) * 1.204 # calculate nose height in percent nose_height = yn / self.image_height # set nose to middle if none was detected if nose_height == 0: nose_height = 0.5 # adjust angle if np.abs(angle) > 0.15: assert self.drone( moveBy(0, 0, 0, angle) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() # adjust height elif nose_height < 0.4 and self.flight_height < self.max_height: self.flight_height += 0.15 assert self.drone( moveBy(0.0, 0, -0.15, 0) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() time.sleep(0.4) elif nose_height > 0.6 and self.flight_height > 0: self.flight_height -= 0.15 assert self.drone( moveBy(0.0, 0, 0.15, 0) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() time.sleep(0.4) # adjust distance elif distance > self.min_dist: assert self.drone( moveBy(min(0.2, distance - self.min_dist), 0, 0, 0) >> FlyingStateChanged(state="hovering", _timeout=5)).wait( ).success() else: assert self.drone( moveBy(0, 0, 0, 0.3) >> FlyingStateChanged( state="hovering", _timeout=5)).wait().success() # returns true if 4 of the 5 last pose predictions were stop def check_for_pause(self): pose_predictions = np.array(self.pose_predictions, dtype=object) prediction_counts = np.asarray( (np.unique(pose_predictions, return_counts=True)), dtype=object).T for i in range(prediction_counts.shape[0]): if prediction_counts[i, 0] > 1 and prediction_counts[i, 1] > 3: return True return False # thread for keypoint predictions def make_predictions(self): while 1: # check if camera stream has started if not np.array_equal(self.last_frame, np.zeros( (1, 1, 3), np.uint8)): # get detections start = time.time() keypoint, self.keypoints_image = self.detector.keypoint_detection( self.last_frame) logging.info( "time for prediction = {:0.3f}".format(time.time() - start)) # check if detection returned results if keypoint.size > 2: self.keypoints.append(keypoint) pred = self.poses_model.predict(keypoint[0].reshape(1, -1)) self.pose_predictions.append(int(pred[0])) if pred > 1: # stop moving if prediction is stop logging.info("canceling move to;{:.3f};{:.3f}".format( self.get_face_distance(), time.time() - event_time)) self.drone(CancelMoveBy()).wait() # put prediction on image cv2.putText(self.keypoints_image, 'Classpred: ' + "{:.0f}".format(pred[0]), (0, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) if self.save_image: cv2.imwrite( "images/" + str(self.image_counter) + ".jpg", self.keypoints_image) string = str(self.image_counter) + ";" self.image_counter += 1 for i in range(keypoint.shape[1]): string += "{:.2f};{:.2f};".format( keypoint[0, i, 0], keypoint[0, i, 1]) self.pose_file.write(string + "\n") # delete from last results if there is no detection elif len(self.keypoints) > 0: self.keypoints.popleft() self.pose_predictions.popleft() if len(self.keypoints) > 1 and self.keypoints[-1].size > 2: # arr = np.array(self.keypoints, dtype=object) # left_eye = remove_outliers(arr[:, 0, self.left_eye]) # if len(np.shape(left_eye)) > 1 and np.shape(left_eye)[0] > 1: # left_eye = np.average(left_eye, axis=0) # right_eye = remove_outliers(arr[:, 0, self.right_eye]) # if len(np.shape(right_eye)) > 1 and np.shape(right_eye)[0] > 1: # right_eye = np.average(right_eye, axis=0) # left_eye = left_eye.reshape((-1)) # right_eye = right_eye.reshape((-1)) # # eye_dist = 24.27 / np.max( # # [0.001, np.power(get_distance(np.average(arr[-2:], axis=0), self.left_eye, self.right_eye), # # 0.753)]) # if len(left_eye) > 1 and len(right_eye) > 1: # self.eye_dist = 24.27 / np.max( # [0.001, # np.power(math.sqrt((left_eye[0] - right_eye[0]) ** 2 + (left_eye[1] - right_eye[1]) ** 2), # 0.753)]) # display face distance cv2.putText( self.keypoints_image, 'Distance(face): ' + "{:.2f}".format(self.get_face_distance()) + "m", (0, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) else: # wait for stream time.sleep(1)
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()
import logging import yaml from detect import Detector config_dir = "./" # Load configuration. config_file_path = config_dir + 'config.yml' config = yaml.safe_load(open(config_file_path, 'r')) # start_detect(config) if __name__ == "__main__": print( "====================================FCD Started====================================" ) LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" logging.basicConfig(filename='logs.log', level=logging.DEBUG, format=LOG_FORMAT) detector = Detector(config) detector.start_detect()
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]
loc.tube = [(0, 271, 33, 76), (0, 271, 78, 115)] loc.window = [[(47, 80, 43, 61), (82, 115, 42, 58), (117, 151, 39, 58), (153, 186, 41, 61), (188, 220, 43, 61)], [(2, 35, 88, 108), (35, 69, 85, 105), (72, 105, 84, 105), (108, 142, 84, 104), (145, 178, 84, 104), (179, 213, 87, 108), (214, 246, 89, 109)]] loc.scale = [[110, 199], [102, 199]] tube0 = Detector(loc.tube[0], loc.window[0], loc.scale[0], 210, 50) tube1 = Detector(loc.tube[1], loc.window[1], loc.scale[1], 160, 33) tube0.feed(frame) tube1.feed(frame) if not count % 20: tube0.update_backgrounds() tube1.update_backgrounds() level = int(tube0.cur_level) cv2.line(frame, (45, level), (60, level), (0, 0, 255), 1) level = int(tube1.cur_level) cv2.line(frame, (90, level), (105, level), (0, 0, 255), 1) text0 = 'level0: ' + str(round(tube0.level_scale)) text1 = 'level1: ' + str(round(tube1.level_scale)) cv2.putText(frame, text0, (5, 13), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.6, (0, 0, 255), 1)
from detect import Detector from imgUtils import ImgUtils if __name__ == "__main__": param = { (1, 'postbake'): { 'expectedWidth': 120, 'expectedHeight': 110, # detection params 'transformationTarget': 'cool', # select correct image transformations 'upperKillzone': 550, 'lowerKillzone': 220, # select correct tracking parameters 'rightKillzone': 3000, 'leftKillzone': -3000, # select correct tracking parameters 'timeToDie': 1, 'timeToLive': 0, 'partioningRequired': True } } D = Detector(**param.get((1, 'postbake'))) for i in range(49, 100): img = cv2.imread("PostBakeSamples/postbake" + str(i) + ".png") x = D.getImgWithBoxes(img) while True: ImgUtils.show("x", x, 0, 0) key = cv2.waitKey(30) if key == ord('q'): break
import argparse import platform from detect import Detector if __name__ == '__main__': parser = argparse.ArgumentParser(description='Start for face recognition client') parser.add_argument('--ip', help='Ip of recognition server') parser.add_argument('--port', help='Port of recognition server') args = parser.parse_args() detector = Detector(args.ip, args.port) if platform.system() == 'Windows' or platform.system() == 'Darwin': import win_recorder win_recorder.record_forever(detector) else: import pi_recorder pi_recorder.record_forever(detector)
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()
from detect import Detector import cv2 detector = Detector(resolution=320) loop = True detect_path = 'imgs/img1.jpg' path, objects, image = detector.detect_objects(detect_path) cv2.imwrite('detected_image.jpg', image) image_np = cv2.imread('detected_image.jpg') cv2.imshow('images', image_np) cv2.waitKey()
def work(self, im): super(HandCropper, self).work(im) self.thresholdGrey(150) self.findContours() self.filterSudoku() # minAreaRect旋转矩形, 筛选出九个九宫格块的轮廓 self.alignPlank() # 透视变换, 对齐并切割出大板(包含整个九宫格和七段管数字板) self.splitSudokuLight() self.threshold_im = self.threshold_sudoku_im self.findRecContours() self.filterRecContoursByArea(9) # boundingRect, 筛选出九宫格块 self.rec_contours = self.resumeOrder(self.rec_contours[:9], ykey=lambda it: it[1], xkey=lambda it: it[0]) self.cropRecContours(self.rec_contours, base_im=self.threshold_sudoku_im) if SAVE: self.saveCropResults() self.saveCropResults(results=[self.threshold_light_im]) return self.results if __name__ == '__main__': # d = Detector(LightCropper(), None) # d.work('../test_im/52648.jpg') d = Detector(HandCropper(), None) d.work('../test_im/real1.jpg')
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)
def worker(): provider = ServerAudioProvider(queue) detector = Detector(provider) detector.detect()
min = distance f1 = f print("Face recognised", str(f1[:-4])) cv2.rectangle(img1, (0, img1.shape[0]), (img1.shape[1], 0), (0, 255, 0), 3) font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img1, str(f1[:-4]), (30, img1.shape[0] - 20), font, 1, (255, 255, 255), 2, cv2.LINE_AA) cv2.imwrite("result.jpg", img1) # return [1,str(f[:-4])] # return [0,""] obj2 = Detector() # obj2.detection() obj1 = Recognition() obj1.initialise("") while True: t1 = threading.Thread(target=obj2.detection()) t2 = threading.Thread(target=obj1.printResults("")) t1.start() t2.start() t1.join() t2.join() # while True: # obj1=Recognition() # t1.start() # t1.join()
def detect(): data = request.get_data(cache=False, as_text=False, parse_form_data=False) provider = BasicAudioProvider(data) detector = Detector(provider) return detector.classify()