def app(video_link, video_name, show, flip_hor, flip_ver): # initialize Face Detection net face_detector = FaceDetector() LOG.info('Face Detector initialization done') # initialize Face Landmark net face_landmarker = FaceLandmarker() LOG.info('Face Landmarker initialization done') # initialize Video Capturer cap = cv.VideoCapture(video_link) (W, H), FPS = imgproc.cameraCalibrate(cap, size=720, by_height=True) LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS)) while cap.isOpened(): _, frm = cap.read() if not _: LOG.info('Reached the end of Video source') break if flip_ver: frm = cv.flip(frm, 0) if flip_hor: frm = cv.flip(frm, 1) frm = imgproc.resizeByHeight(frm, 720) _start_t = time.time() scores, bboxes = face_detector.getFaces(frm, def_score=0.5) landmarks = [] for i, bbox in enumerate(bboxes): x1, y1, x2, y2 = bbox face_img = frm[y1:y2, x1:x2] landmark = face_landmarker.getLandmark(face_img) landmark[:, :] += np.array([x1, y1]) landmarks.append(landmark) _prx_t = time.time() - _start_t if len(bboxes): for i, landmark in enumerate(landmarks): for j, point in enumerate(landmark): cv.circle(frm, tuple(point), 3, (0, 255, 0), -1) frm = vis.plotBBoxes(frm, bboxes, len(bboxes) * ['face'], scores) frm = vis.plotInfo(frm, 'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t)) frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB) if show: cv.imshow(video_name, frm) key = cv.waitKey(1) if key in [27, ord('q')]: LOG.info('Interrupted by Users') break cap.release() cv.destroyAllWindows()
def app(image_path): # initialize Face Detection net face_detector = FaceDetector() LOG.info('Face Detector initialization done') # initialize Face Landmark net face_landmarker = FaceLandmarker() LOG.info('Face Landmarker initialization done') cap = cv.VideoCapture(0) while cap.isOpened(): _, image = cap.read() if not _: LOG.info('Reached the end of Video source') break image = imgproc.resizeByHeight(image, 720) _start_t = time.time() scores, bboxes = face_detector.getFaces(image, def_score=0.5) landmarks = [] for i, bbox in enumerate(bboxes): x1, y1, x2, y2 = bbox face_img = image[y1:y2, x1:x2] landmark = face_landmarker.getLandmark(face_img) aligned_img = alignFace(face_img, landmark) cv.imshow('aligned-faces' + str(i), aligned_img) landmark[:, :] += np.array([x1, y1]) landmarks.append(landmark) _prx_t = time.time() - _start_t if len(bboxes): for i, landmark in enumerate(landmarks): for j, point in enumerate(landmark): cv.circle(image, tuple(point), 3, (0, 255, 0), -1) image = vis.plotBBoxes(image, bboxes, len(bboxes) * ['face'], scores) image = vis.plotInfo(image, 'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t)) image = cv.cvtColor(np.asarray(image), cv.COLOR_BGR2RGB) cv.imshow(image_path, image) key = cv.waitKey(1) if key in [27, ord('q')]: LOG.info('Interrupted by Users') break cap.release() cv.destroyAllWindows()
def app(video_link, video_name, show, record, flip_hor, flip_ver): # initialize Face Detection net object_detector = ObjectDetector() # initialize Video Capturer cap = cv.VideoCapture(video_link) (W, H), FPS = imgproc.cameraCalibrate(cap) LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS)) if record: time_str = time.strftime(cfg.TIME_FM) writer = cv.VideoWriter(video_name + time_str + '.avi', cv.VideoWriter_fourcc(*'XVID'), 20, (1280, 720)) cnt_frm = 0 while cap.isOpened(): _, frm = cap.read() if not _: LOG.info('Reached the end of Video source') break cnt_frm += 1 if flip_ver: frm = cv.flip(frm, 0) if flip_hor: frm = cv.flip(frm, 1) frm = imgproc.resizeByHeight(frm, 720) _start_t = time.time() scores, bboxes = object_detector.getObjects(frm, def_score=0.5) _prx_t = time.time() - _start_t if len(bboxes): frm = vis.plotBBoxes(frm, bboxes, len(bboxes) * ['person'], scores) frm = vis.plotInfo(frm, 'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t)) frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB) if record: writer.write(frm) if show: cv.imshow(video_name, frm) key = cv.waitKey(1) if key in [27, ord('q')]: LOG.info('Interrupted by Users') break if record: writer.release() cap.release() cv.destroyAllWindows()
def app(image_path): # initialize Face Detection net face_detector = FaceDetector() LOG.info('Face Detector initialization done') # initialize Face Landmark net face_landmarker = FaceLandmarker() LOG.info('Face Landmarker initialization done') # initialize Face Alignment class face_aligner = FaceAligner() LOG.info('Face Aligner initialization done') # initialize Video Capturer image = cv.imread(image_path) assert image is not None image = imgproc.resizeByHeight(image, 720) _start_t = time.time() scores, bboxes = face_detector.getFaces(image, def_score=0.5) landmarks = [] for i, bbox in enumerate(bboxes): x1, y1, x2, y2 = bbox face_img = image[y1:y2, x1:x2] landmark = face_landmarker.getLandmark(face_img) aligned_img = face_aligner.align(face_img, landmark) cv.imshow('aligned-faces' + str(i), aligned_img) landmark[:, :] += np.array([x1, y1]) landmarks.append(landmark) _prx_t = time.time() - _start_t if len(bboxes): for i, landmark in enumerate(landmarks): for j, point in enumerate(landmark): cv.circle(image, tuple(point), 3, (0, 255, 0), -1) image = vis.plotBBoxes(image, bboxes, len(bboxes) * ['face'], scores) image = vis.plotInfo(image, 'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t)) image = cv.cvtColor(np.asarray(image), cv.COLOR_BGR2RGB) cv.imshow(image_path, image) key = cv.waitKey(0) cv.destroyAllWindows()
def app(video_link, video_name, show, record, flip_hor, flip_ver): # initialize Face Detection net object_detector = ObjectDetector() # initialize Video Capturer #cap = cv.VideoCapture(video_link) cap = WebcamVideoStream(src=cfg.RTSP_URL).start() # (W, H), FPS = imgproc.cameraCalibrate(cap.stream) # LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS)) if record: time_str = time.strftime(cfg.TIME_FM) writer = cv.VideoWriter(video_name + time_str + '.avi', cv.VideoWriter_fourcc(*'XVID'), 20, (1280, 720)) #cnt_frm = 0 while True: frm = cap.read() if frm is None: continue #cnt_frm += 1 # if(cnt_frm % 5 != 0): # continue if flip_ver: frm = cv.flip(frm, 0) if flip_hor: frm = cv.flip(frm, 1) frm = imgproc.resizeByHeight(frm, 720) _start_t = time.time() images, bboxes = object_detector.getObjects(frm, def_score=0.5) if images: files = [] for im in images: byte_io = BytesIO() im.save(byte_io, 'png') byte_io.seek(0) files.append(('files', byte_io)) files.append( ('bboxes', (None, json.dumps(bboxes), 'application/json'))) if (len(bboxes)): try: requests.post(url=URL, files=files) except: continue _prx_t = time.time() - _start_t if len(bboxes): frm = vis.plotBBoxes(frm, bboxes, len(bboxes) * ['person']) frm = vis.plotInfo(frm, 'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t)) frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB) if record: writer.write(frm) # if show: # cv.imshow(video_name, frm) # key = cv.waitKey(1) # if key in [27, ord('q')]: # LOG.info('Interrupted by Users') # break if record: writer.release()
def app(video_link, video_name, show, record, flip_hor, flip_ver): # initialize Face Detection net config = read_py_config('config.py') object_detector = ObjectDetector() reid = PersonEmbedder() # initialize Video Capturer cap = MulticamCapture(video_link) #cap = WebcamVideoStream(src=video_link).start() # (W, H), FPS = imgproc.cameraCalibrate(cap) # LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS)) tracker = MultiCameraTracker(cap.get_num_sources(), reid, **config) thread_body = FramesThreadBody(cap, max_queue_length=len(cap.captures) * 2) frames_thread = Thread(target=thread_body) frames_thread.start() if record: time_str = time.strftime(cfg.TIME_FM) writer = cv.VideoWriter(video_name + time_str + '.avi', cv.VideoWriter_fourcc(*'XVID'), 20, (1280, 720)) cnt_frm = 0 counter = 0 while thread_body.process: try: frm = thread_body.frames_queue.get_nowait() except queue.Empty: frm = None if frm is None: continue cnt_frm += 1 # if flip_ver: frm = cv.flip(frm, 0) # if flip_hor: frm = cv.flip(frm, 1) _start_t = time.time() all_detections = [] for f in frm: f = imgproc.resizeByHeight(f, 640) _, bboxes = object_detector.getObjects(f, def_score=0.5) all_detections.append(bboxes) tracker.process(frm, all_detections, [[]]) tracked_objects = tracker.get_tracked_objects() _prx_t = time.time() - _start_t fps = round(1 / _prx_t, 1) if len(bboxes): frm = visualize_multicam_detections(frm, tracked_objects, fps) frm = vis.plotInfo(frm, 'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t)) frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB) if record: writer.write(frm) if show: cv.imshow(video_name, frm) key = cv.waitKey(1) if key in [27, ord('q')]: LOG.info('Interrupted by Users') break else: if (counter % 10 == 0): print( f"IN : {SingleCameraTracker.COUNT_IN}, OUT: {SingleCameraTracker.COUNT_OUT}" ) counter += 1 thread_body.process = False frames_thread.join()
def app(video_path, video_name, show=False, record=False): detector = FaceDetector() plugin = detector.getPlugin() poser = HeadPoser(plugin=plugin) cap = cv.VideoCapture(video_path) (W, H), FPS = imgproc.cameraCalibrate(cap) _, frm = cap.read() frm = imgproc.resizeByHeight(frm) H, W = frm.shape[:2] FRM_MOD = int(1. * FPS / cfg.pFPS + 0.5) LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS)) if record: time_str = time.strftime(cfg.TIME_FM) writer = cv.VideoWriter(video_name + time_str + '.avi', cv.VideoWriter_fourcc(*'XVID'), FPS, (W, H)) cnt_frm = 0 while cap.isOpened(): _, frm = cap.read() if not _: LOG.info('Reached the end of Video source') break cnt_frm += 1 if cnt_frm % FRM_MOD: continue print(cnt_frm) frm = imgproc.resizeByHeight(frm) _start_t = time.time() scores, bboxes = detector.getFaces(frm) if len(bboxes): for i, bbox in enumerate(bboxes): x1, y1, x2, y2 = bbox face_image = frm[y1:y2, x1:x2] yaw, pitch, roll = poser.estimatePose(face_image) cpoint = [(x1 + x2) // 2, (y1 + y2) // 2] frm = hvis.draw(frm, cpoint, (yaw, pitch, roll)) _prx_t = time.time() - _start_t frm = vis.plotInfo(frm, 'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t)) frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB) if record: writer.write(frm) if show: cv.imshow(video_name, frm) key = cv.waitKey(1) if key in [27, ord('q')]: LOG.info('Interrupted by Users') break if record: writer.release() cap.release() cv.destroyAllWindows()
def app(video_link, video_name, show, record, flip_hor, flip_ver): # initialize Face Detection net config = read_py_config('config.py') object_detector = ObjectDetector() reid = PersonEmbedder() # initialize Video Capturer #cap = MulticamCapture(video_link) cap = WebcamVideoStream(src=video_link).start() (W, H), FPS = imgproc.cameraCalibrate(cap) LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS)) tracker = MultiCameraTracker(1, reid, **config) if record: time_str = time.strftime(cfg.TIME_FM) writer = cv.VideoWriter(video_name + time_str + '.avi', cv.VideoWriter_fourcc(*'XVID'), 20, (1280, 720)) cnt_frm = 0 counter = 0 while True: frm = cap.read() if frm is None: continue cnt_frm += 1 # if flip_ver: frm = cv.flip(frm, 0) # if flip_hor: frm = cv.flip(frm, 1) _start_t = time.time() all_detections = [] frm = imgproc.resizeByHeight(frm, 640) _, bboxes = object_detector.getObjects(frm, def_score=0.5) all_detections.append(bboxes) tracker.process([frm], all_detections, [[]]) tracked_objects = tracker.get_tracked_objects() _prx_t = time.time() - _start_t fps = round(1 / _prx_t, 1) if len(bboxes): frm = visualize_multicam_detections([frm], tracked_objects, fps) frm = vis.plotInfo(frm, 'Raspberry Pi - FPS: {:.3f}'.format(1 / _prx_t)) frm = cv.cvtColor(np.asarray(frm), cv.COLOR_BGR2RGB) if record: writer.write(frm) if show: cv.imshow(video_name, frm) key = cv.waitKey(1) if key in [27, ord('q')]: LOG.info('Interrupted by Users') break else: if (counter % 10 == 0): print( f"IN : {SingleCameraTracker.COUNT_IN}, OUT: {SingleCameraTracker.COUNT_OUT}" ) counter += 1 if record: writer.release() cap.release() cv.destroyAllWindows()
def app(video_link, video_name, show, flip_hor, flip_ver): video_links = [ '/home/pi/Videos/crowd-6582.mp4', '/home/pi/Videos/india-444.mp4', '/home/pi/Videos/paris-2174.mp4', '/home/pi/Videos/scotland-21847.mp4', ] # initialize Face Detection net face_detector = FaceDetector() LOG.info('Face Detector initialization done') # initialize Face Landmark net face_landmarker = FaceLandmarker() LOG.info('Face Landmarker initialization done') # initialize Video Capturer cap0 = cv.VideoCapture(video_links[0]) cap1 = cv.VideoCapture(video_links[1]) cap2 = cv.VideoCapture(video_links[2]) cap3 = cv.VideoCapture(video_links[3]) # (W, H), FPS = imgproc.cameraCalibrate(cap, size=720, by_height=True) # LOG.info('Camera Info: ({}, {}) - {:.3f}'.format(W, H, FPS)) time_str = time.strftime(cfg.TIME_FM) saved_path = 'output.avi' writer = cv.VideoWriter(saved_path, cv.VideoWriter_fourcc(*'XVID'), 24, (1280, 720)) cnt_frm = 0 while cap0.isOpened() and cap1.isOpened() and cap2.isOpened( ) and cap3.isOpened(): _0, frm0 = cap0.read() _1, frm1 = cap1.read() _2, frm2 = cap2.read() _3, frm3 = cap3.read() if not _0 or not _1 or not _2 or not _3: LOG.info('Reached the end of Video source') break cnt_frm += 1 frm0 = imgproc.resizeByHeight(frm0, 360) frm1 = imgproc.resizeByHeight(frm1, 360) frm2 = imgproc.resizeByHeight(frm2, 360) frm3 = imgproc.resizeByHeight(frm3, 360) frm0 = processFrame(frm0, face_detector, face_landmarker) frm1 = processFrame(frm1, face_detector, face_landmarker) frm2 = processFrame(frm2, face_detector, face_landmarker) frm3 = processFrame(frm3, face_detector, face_landmarker) frm = np.zeros((720, 1280, 3)) frm[:360, :640] = frm0 frm[:360, 640:] = frm1 frm[360:, :640] = frm2 frm[360:, 640:] = frm3 LOG.info('frm shape: {}'.format(frm.shape)) cv.imwrite(str(cnt_frm) + '.jpg', frm) writer.write(frm) LOG.info('Frames processed: {}'.format(cnt_frm)) if show: cv.imshow('output', frm) key = cv.waitKey(1) if key in [27, ord('q')]: LOG.info('Interrupted by Users') break writer.release() cap0.release() cap1.release() cap2.release() cap3.release() cv.destroyAllWindows()