def calculate_fps(self, frames_no=100): fps = FPS().start() # Don't wanna display window if self.debug: self.debug = not self.debug for i in range(0, frames_no): self.where_lane_be() fps.update() fps.stop() # Don't wanna display window if not self.debug: self.debug = not self.debug print('Time taken: {:.2f}'.format(fps.elapsed())) print('~ FPS : {:.2f}'.format(fps.fps()))
class PiVideoStream: def __init__(self, resolution=(400,200), framerate=60): #Start up camera self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.camera.rotation = -90 self.rawCap = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCap, format="bgr", use_video_port=True) self.frame = None self.stopped = False self.fps = FPS().start() def start(self): Thread(target=self.update, args=()).start() return self def update(self): # Continually grab frames from camera for f in self.stream: self.frame = f.array self.rawCap.truncate(0) self.fps.update() if self.stopped: self.stream.close() self.rawCap.close() self.camera.close() return def read(self): return self.frame def stop(self): self.fps.stop() self.stopped = True def getFPS(self): return self.fps.fps()
def update(self): # keep looping infinitely until the thread is stopped print("[INFO] Waiting for Feed") fps = FPS().start() for f in self.stream: #print("[INFO] Reading Feed") self.frameCaptured = True # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array fps.update() self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() fps.stop() #print("Thread FPS: {: .2f}".format(fps.fps())) return
def corn_tracking_opticalflow( frame_dir, model_path="./output/faster-rcnn-corn_bgr8_ep100.pt", frame_count=80, output_file=None): # Labels of Network. labels = {0: 'background', 1: 'corn'} lk_params = dict(winSize=(50, 50), maxLevel=4, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) total_frames = 1 # prev_frame_number = format(int(frame_path[-11:-4])-1, '07d') # prev_frame_path = frame_path[:-11] + prev_frame_number # prev_frame = cv2.imread(prev_frame_path) # frame = cv2.imread(frame_path) prev_frame_number = format(1, '07d') prev_frame_path = os.path.join(frame_dir, 'frame_' + prev_frame_number + '.png') prev_frame = cv2.imread(prev_frame_path) fps = FPS().start() tracking_started = False if output_file: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(output_file, fourcc, 100, (prev_frame.shape[1], prev_frame.shape[0]), True) # color_dict = dict() frame_number = 2 corn_id_bbox_dict = dict() while True: frame = cv2.imread( os.path.join(frame_dir, 'frame_' + format(frame_number, '07d') + '.png')) if frame is None: #end of video file break # running the object detector every nth frame if total_frames % int(frame_count) - 1 == 0: pred_boxes, pred_class, pred_score = get_prediction( frame, model_path, 0.5) centroids = np.zeros([1, 1, 2], dtype=np.float32) # only if there are predictions if pred_boxes != None: corn_dict = dict() for i in range(len(pred_boxes)): corn_dict[i] = dict() corn_dict['centroids'] = dict() for i in range(len(pred_boxes)): # cv2.rectangle(img, boxes[i][0], boxes[i][1],color=(0, 255, 0), thickness=rect_th) color = list(np.random.random(size=3) * 256) # print("i color", i, color) tracking_id = int(i) confidence = pred_score[i] xLeftBottom = int(pred_boxes[i][0][0]) yLeftBottom = int(pred_boxes[i][0][1]) xRightTop = int(pred_boxes[i][1][0]) yRightTop = int(pred_boxes[i][1][1]) # print class and confidence label = pred_class[i] + ": " + str(confidence) # print(label) x = (xLeftBottom + xRightTop) / 2 y = (yLeftBottom + yRightTop) / 2 corn_dict[i]['bbox'] = [(xLeftBottom, yLeftBottom), (xRightTop, yRightTop)] corn_dict[i]['centroid'] = [(x, y)] corn_dict['centroids'][tuple((x, y))] = [] frame = cv2.rectangle(frame, (xLeftBottom, yLeftBottom), (xRightTop, yRightTop), color, thickness=2) ### added today # draw the centroid on the frame frame = cv2.circle(frame, (int(x), int(y)), 15, color, -1) print("before if STATE i %d frame %d x y: %d %d" % (i, total_frames, x, y)) tracking_started = True if i == 0: color_dict = dict() centroids[0, 0, 0] = x centroids[0, 0, 1] = y color_dict[tuple(color)] = [(x, y)] else: centroid = np.array([[[x, y]]], dtype=np.float32) centroids = np.append(centroids, centroid, axis=0) color_dict[tuple(color)] = [(x, y)] else: # track an object only if it has been detected if centroids.sum() != 0 and tracking_started: next1, st, error = cv2.calcOpticalFlowPyrLK( prev_frame, frame, centroids, None, **lk_params) good_new = next1[st == 1] good_old = centroids[st == 1] # print("color dict", color_dict) corn_id_bbox = dict() for i, (new, old) in enumerate(zip(good_new, good_old)): # Returns a contiguous flattened array as (x, y) coordinates for new point a, b = new.ravel() c, d = old.ravel() distance = np.sqrt((a - c)**2 + (b - d)**2) # distance between new and old points should be less than # 200 for 2 points to be same the object if distance < 200: corn_dict['centroids'][corn_dict[i]['centroid'] [0]].append((a, b)) for color, centroids_list in color_dict.items(): for centroids in centroids_list: if centroids == (c, d): color_dict[color].append((a, b)) color_old = color frame = cv2.circle(frame, (a, b), 15, color_old, -1) ## TODO: global corn ID? res = tuple( map(operator.sub, (c, d), corn_dict[i]['centroid'][0])) new_bbox_coor1 = tuple( map(operator.add, corn_dict[i]['bbox'][0], res)) new_bbox_coor2 = tuple( map(operator.add, corn_dict[i]['bbox'][1], res)) new_bbox_coor1 = tuple(map(int, new_bbox_coor1)) new_bbox_coor2 = tuple(map(int, new_bbox_coor2)) frame = cv2.rectangle(frame, new_bbox_coor1, new_bbox_coor2, color_old, thickness=2) ### added today frame = cv2.putText(frame, str(total_frames), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 10, cv2.LINE_AA) # corn_id_bbox.append([new_bbox_coor1, new_bbox_coor2]) corn_id_bbox[i] = [new_bbox_coor1, new_bbox_coor2] print("total fr", total_frames, "corn_id", corn_id_bbox) corn_id_bbox_dict[frame_number] = corn_id_bbox centroids = good_new.reshape(-1, 1, 2) total_frames += 1 frame_number += 1 fps.update() fps.stop() print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) if output_file: writer.write(frame) cv2.namedWindow("frame", cv2.WINDOW_NORMAL) cv2.imshow("frame", frame) prev_frame = frame if cv2.waitKey(1) >= 0: # Break with ESC break return corn_id_bbox_dict
print('Reconocido ',item[4]) # if item[3] == 'unknown': # pickled = codecs.encode(pickle.dumps(item[0]), "base64").decode() # addperson2db(name='', surname='', is_recongized=False, # last_in=dt.datetime.utcnow(), last_out='', # picture=pickled, likehood=0) # else: # separador = item[3].index("_") # addperson2db(name=item[3][0:separador], # surname=item[3][separador+1:-1], is_recongized=True, # last_in=dt.datetime.utcnow(), last_out='', # picture='', likehood=item[4]) # frame = draw_frame(frame, item) fps_count.update() cpt += 1 out_prev = out # if cpt > 250: # video_getter.stop() # break # exitbool = show_frame(frame) if exitbool or cpt > 100: SV.stop() fps_count.stop() print("[INFO] elasped time fps processed: {:.2f}".format(fps_count.elapsed())) print("[INFO] approx. processed FPS: {:.2f}".format(fps_count.fps())) time.sleep(1) video_getter.stop() # db_client.close() break
class Main: def __init__(self, file=None, camera=False): print("Loading pipeline...") self.file = file self.camera = camera self.pipeline = create_pipeline(self.camera) self.start_pipeline() self.COUNTER = 0 self.mCOUNTER = 0 self.hCOUNTER = 0 self.TOTAL = 0 self.mTOTAL = 0 self.hTOTAL = 0 self.fps = FPS() self.fps.start() def start_pipeline(self): self.device = depthai.Device(self.pipeline) print("Starting pipeline...") if self.camera: self.cam_out = self.device.getOutputQueue("cam_out", 4, False) else: self.face_in = self.device.getInputQueue("face_in", 4, False) self.face_nn = self.device.getOutputQueue("face_nn", 4, False) self.land68_in = self.device.getInputQueue("land68_in", 4, False) self.land68_nn = self.device.getOutputQueue("land68_nn", 4, False) def draw_bbox(self, bbox, color): cv2.rectangle(self.debug_frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2) def run_face(self): if self.camera: nn_data = self.face_nn.tryGet() else: nn_data = run_nn(self.face_in, self.face_nn, {"data": to_planar(self.frame, (300, 300))}) results = to_bbox_result(nn_data) self.face_coords = [ frame_norm(self.frame, *obj[3:7]) for obj in results if obj[2] > 0.4 ] if len(self.face_coords) == 0: return False if len(self.face_coords) > 0: for face_coord in self.face_coords: face_coord[0] -= 15 face_coord[1] -= 15 face_coord[2] += 15 face_coord[3] += 15 self.face_frame = [ self.frame[face_coord[1]:face_coord[3], face_coord[0]:face_coord[2]] for face_coord in self.face_coords ] if debug: for bbox in self.face_coords: self.draw_bbox(bbox, (10, 245, 10)) return True def run_land68(self, face_frame, count): try: nn_data = run_nn(self.land68_in, self.land68_nn, {"data": to_planar(face_frame, (160, 160))}) out = to_nn_result(nn_data) result = frame_norm(face_frame, *out) eye_left = [] eye_right = [] mouth = [] hand_points = [] for i in range(72, 84, 2): eye_left.append((out[i], out[i + 1])) for i in range(84, 96, 2): eye_right.append((out[i], out[i + 1])) for i in range(96, len(result), 2): if i == 100 or i == 116 or i == 104 or i == 112 or i == 96 or i == 108: mouth.append(np.array([out[i], out[i + 1]])) for i in range(16, 110, 2): if i == 16 or i == 60 or i == 72 or i == 90 or i == 96 or i == 108: cv2.circle(self.debug_frame, (result[i] + self.face_coords[count][0], result[i + 1] + self.face_coords[count][1]), 2, (255, 0, 0), thickness=1, lineType=8, shift=0) hand_points.append( (result[i] + self.face_coords[count][0], result[i + 1] + self.face_coords[count][1])) ret, rotation_vector, translation_vector, camera_matrix, dist_coeffs = get_pose_estimation( self.frame.shape, np.array(hand_points, dtype='double')) ret, pitch, yaw, roll = get_euler_angle(rotation_vector) (nose_end_point2D, jacobian) = cv2.projectPoints(np.array([(0.0, 0.0, 1000.0)]), rotation_vector, translation_vector, camera_matrix, dist_coeffs) p1 = (int(hand_points[1][0]), int(hand_points[1][1])) p2 = (int(nose_end_point2D[0][0][0]), int(nose_end_point2D[0][0][1])) cv2.line(self.debug_frame, p1, p2, (255, 0, 0), 2) euler_angle_str = 'Y:{}, X:{}, Z:{}'.format(pitch, yaw, roll) cv2.putText(self.debug_frame, euler_angle_str, (10, 20), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.5, (0, 0, 255)) if pitch < 0: self.hCOUNTER += 1 if self.hCOUNTER >= 20: cv2.putText(self.debug_frame, "SLEEP!!!", (self.face_coords[count][0], self.face_coords[count][1] - 10), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 3) else: if self.hCOUNTER >= 3: self.hTOTAL += 1 self.hCOUNTER = 0 left_ear = self.eye_aspect_ratio(eye_left) right_ear = self.eye_aspect_ratio(eye_right) ear = (left_ear + right_ear) / 2.0 if ear < 0.15: # Eye aspect ratio:0.15 self.COUNTER += 1 if self.COUNTER >= 20: cv2.putText(self.debug_frame, "SLEEP!!!", (self.face_coords[count][0], self.face_coords[count][1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) else: # If it is less than the threshold 3 times in a row, it means that an eye blink has been performed if self.COUNTER >= 3: # Threshold: 3 self.TOTAL += 1 # Reset the eye frame counter self.COUNTER = 0 mouth_ratio = self.mouth_aspect_ratio(mouth) if mouth_ratio > 0.5: self.mCOUNTER += 1 else: if self.mCOUNTER >= 3: self.mTOTAL += 1 self.mCOUNTER = 0 cv2.putText( self.debug_frame, "eye:{:d},mouth:{:d},head:{:d}".format(self.TOTAL, self.mTOTAL, self.hTOTAL), (10, 40), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.5, ( 255, 0, 0, )) if self.TOTAL >= 50 or self.mTOTAL >= 15 or self.hTOTAL >= 10: cv2.putText(self.debug_frame, "Danger!!!", (100, 200), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) except: pass def mouth_aspect_ratio(self, mouth): A = np.linalg.norm(mouth[1] - mouth[5]) # 51, 59 B = np.linalg.norm(mouth[2] - mouth[4]) # 53, 57 C = np.linalg.norm(mouth[0] - mouth[3]) # 49, 55 mar = (A + B) / (2.0 * C) return mar def eye_aspect_ratio(self, eye): A = euclidean(eye[1], eye[5]) B = euclidean(eye[2], eye[4]) C = euclidean(eye[0], eye[3]) return (A + B) / (2.0 * C) def parse(self): if debug: self.debug_frame = self.frame.copy() face_success = self.run_face() if face_success: for i in range(len(self.face_frame)): self.run_land68(self.face_frame[i], i) self.fps.update() if debug: aspect_ratio = self.frame.shape[1] / self.frame.shape[0] cv2.imshow( "Camera_view", cv2.resize(self.debug_frame, (int(900), int(900 / aspect_ratio)))) if cv2.waitKey(1) == ord('q'): cv2.destroyAllWindows() raise StopIteration() def run_video(self): cap = cv2.VideoCapture(str(Path(self.file).resolve().absolute())) while cap.isOpened(): read_correctly, self.frame = cap.read() if not read_correctly: break try: self.parse() except StopIteration: break cap.release() def run_camera(self): while True: rgb_in = self.cam_out.get() self.frame = np.array(rgb_in.getData()).reshape( (3, rgb_in.getHeight(), rgb_in.getWidth())).transpose(1, 2, 0).astype(np.uint8) try: self.parse() except StopIteration: break def run(self): if self.file is not None: self.run_video() else: self.run_camera() self.fps.stop() print("FPS:{:.2f}".format(self.fps.fps())) del self.device
def main(): CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) #print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe('MobileNetSSD_deploy.prototxt.txt', 'MobileNetSSD_deploy.caffemodel') #print("[INFO] starting video stream...") c = 0 detect = [] vs = VideoStream(src=2).start() time.sleep(2.0) fps = FPS().start() fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output1.avi', fourcc, 20.0, (640, 480)) while True: frame = vs.read() frame = imutils.resize(frame, width=800) (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) labels = [] net.setInput(blob) detections = net.forward() det_conf = detections[0, 0, :, 2] top_indices = [i for i, conf in enumerate(det_conf) if conf >= 0.2] top_conf = det_conf[top_indices] det_indx = detections[0, 0, :, 1] top_label_indices = det_indx[top_indices].tolist() if c == 0: for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence > 0.40: # extract the index of the class label from the # `detections`, then compute the (x, y)-coordinates of # the bounding box for the object idx = int(detections[0, 0, i, 1]) box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # draw the prediction on the frame label = "{}".format(CLASSES[idx]) cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2) y = startY - 15 if startY - 15 > 15 else startY + 15 if label == 'bottle': cv2.putText(frame, 'Warning', (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 1, COLORS[idx], 2) else: cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) out.write(frame) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break fps.update() fps.stop() #print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) #print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
#Renkler Mavi-Yeşil-Kırmızı kodlamasındadır (ör. 128,128,0) cv2.rectangle(frame, (left, top), (right, bottom), (128, 128, 0), 2) y = top - 15 if top - 15 > 15 else top + 15 cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX, .8, (128, 128, 0), 2) if name == "Furkan": #Eğer özel bir isim bulduysa ledi yak. GPIO.output(11, 1) #Canlı video penceresi cv2.imshow("Yüz Tanıma Sistemi", frame) key = cv2.waitKey(1) & 0xFF #q tuşu ile pencereyi kapatabilirsiniz. if key == ord("q"): break #FPS değerimizi güncelledik. fps.update() fps.stop() #ve durdurduk. print("[-] İşlem Süresi: {:.2f}".format(fps.elapsed())) print("[-] Ortalama FPS: {:.2f}".format(fps.fps())) #FPS'inizin çok düşük olması normal. Ben RPI4 4GB ile ortalama 3-4 FPS alıyorum. cv2.destroyAllWindows() vs.stop()
class DepthAI: def __init__(self,file=None,camera=False): print("Loading pipeline...") self.file = file self.camera = camera self.create_pipeline() self.start_pipeline() self.fps = FPS() self.fps.start() def create_pipeline(self): print("Creating pipeline...") self.pipeline = depthai.Pipeline() if self.camera: print("Creating Color Camera...") self.cam = self.pipeline.createColorCamera() self.cam.setPreviewSize(self._cam_size[0],self._cam_size[1]) self.cam.setResolution(depthai.ColorCameraProperties.SensorResolution.THE_1080_P) self.cam.setInterleaved(False) self.cam.setBoardSocket(depthai.CameraBoardSocket.RGB) cam_xout = self.pipeline.createXLinkOut() cam_xout.setStreamName("cam_out") self.cam.preview.link(cam_xout.input) self.create_nns() def create_nns(self): pass def create_models(self, model_path,model_name,first_model=False): print(f"Start creating{model_path}Neural Networks") model_nn = self.pipeline.createNeuralNetwork() model_nn.setBlobPath(str(Path(model_path).resolve().absolute())) if first_model and self.camera: self.cam.preview.link(model_nn.input) else: model_in = self.pipeline.createXLinkIn() model_in.setStreamName(f"{model_name}_in") model_in.out.link(model_nn.input) model_nn_xout = self.pipeline.createXLinkOut() model_nn_xout.setStreamName(f"{model_name}_nn") model_nn.out.link(model_nn_xout.input) def create_mobilenet_nn(self,model_path,model_name,first_model=False,conf=0.5): print(f"Start creating{model_path}Neural Networks") model_nn = self.pipeline.createMobileNetDetectionNetwork() model_nn.setBlobPath(str(Path(model_path).resolve().absolute())) model_nn.setConfidenceThreshold(conf) model_nn.input.setBlocking(False) if first_model and self.camera: self.cam.preview.link(model_nn.input) else: model_in = self.pipeline.createXLinkIn() model_in.setStreamName(f"{model_name}_in") model_in.out.link(model_nn.input) model_nn_xout = self.pipeline.createXLinkOut() model_nn_xout.setStreamName(f"{model_name}_nn") model_nn.out.link(model_nn_xout.input) def create_yolo_nn(self,model_path,model_name,first_model=False): print(f"Start creating{model_path}Neural Networks") model_nn = self.pipeline.createYoloDetectionNetwork() model_nn.setBlobPath(str(Path(model_path).resolve().absolute())) model_nn.setConfidenceThreshold(conf) model_nn.input.setBlocking(False) if first_model: if self.camera: self.cam.preview.link(model_nn.input) else: model_in = self.pipeline.createXLinkIn() model_in.setStreamName(f"{model_name}_in") model_in.out.link(model_nn.input) model_nn_xout = self.pipeline.createXLinkOut() model_nn_xout.setStreamName(f"{model_name}_nn") model_nn.out.link(model_nn_xout.input) def start_pipeline(self): self.device = depthai.Device(self.pipeline) print("Starting pipeline...") self.device.startPipeline() if self.camera: self.cam_out = self.device.getOutputQueue("cam_out", 4, False) self.start_nns() def start_nns(self): pass def full_frame_cords(self, cords): original_cords = self.face_coords[0] return [ original_cords[0 if i % 2 == 0 else 1] + val for i, val in enumerate(cords) ] def full_frame_bbox(self, bbox): relative_cords = self.full_frame_cords(bbox) height, width = self.frame.shape[:2] y_min = max(0, relative_cords[1]) y_max = min(height, relative_cords[3]) x_min = max(0, relative_cords[0]) x_max = min(width, relative_cords[2]) result_frame = self.frame[y_min:y_max, x_min:x_max] return result_frame, relative_cords def draw_bbox(self, bbox, color): cv2.rectangle(self.debug_frame, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2) def run_video(self): cap = cv2.VideoCapture(str(Path(self.file).resolve().absolute())) while cap.isOpened(): read_correctly, self.frame = cap.read() if not read_correctly: break try: self.parse() except StopIteration: break cap.release() def run_camera(self): while True: in_rgb = self.cam_out.tryGet() if in_rgb is not None: shape = (3,in_rgb.getHeight(),in_rgb.getWidth()) self.frame = in_rgb.getData().reshape(shape).transpose(1, 2, 0).astype(np.uint8) self.frame = np.ascontiguousarray(self.frame) try: self.parse() except StopIteration: break def parse(self): if debug: self.debug_frame = self.frame.copy() self.parse_run() if debug: aspect_ratio = self.frame.shape[1] / self.frame.shape[0] cv2.imshow("Camera_view", cv2.resize(self.debug_frame, ( int(900), int(900 / aspect_ratio)))) if cv2.waitKey(1) == ord('q'): cv2.destroyAllWindows() raise StopIteration() def parse_run(self): pass @property def cam_size(self): return self._cam_size @cam_size.setter def cam_size(self,v): self._cam_size = v @property def get_cam_size(self): return (self._cam_size[0],self._cam_size[1]) def run(self): if self.file is not None: self.run_video() else: self.run_camera() self.fps.stop() print("FPS:{:.2f}".format(self.fps.fps())) del self.device
def main(): program_data = config_file_loader.load(ARGUMENTS['file']) targets = program_data.get('targets') if targets is not None: targets = targets.keys() # Dictionary of counter names specified by the user in the DSL program. # Each key is the name of the counter, and the value is a set, whose elements # are the IDs of the objects detected counters = {} targets_conditions = program_data.get('targets_conditions') activities = program_data.get('activities') if activities is not None: activities = activities.keys() activities_conditions = program_data.get('activities_conditions') # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe( './MobileNetSSD_deploy.prototxt.txt', './MobileNetSSD_deploy.caffemodel') # initialize the video stream, allow the cammera sensor to warmup, # and initialize the FPS counter print("[INFO] starting video stream...") video_source = VideoStream(src=0).start() time.sleep(2.0) fps = FPS().start() centroid_tracker = CentroidTracker() frame1 = video_source.read() frame1 = imutils.resize(frame1, width=600) frame2 = video_source.read() frame2 = imutils.resize(frame2, width=600) cv2.namedWindow("Frame") cv2.moveWindow("Frame", 380, 150) # loop over the frames from the video stream while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = video_source.read() frame = imutils.resize(frame, width=600) # Check if there are activities to look for if activities is not None: activity = detect_activity(frame1, frame2, frame) if activity is not None and activity in activities: check_activity_conditions( activity, activities_conditions, frame) if targets is None: current_date = datetime.datetime.now().strftime(CURRENT_DATE_FORMAT_STRING) cv2.putText(frame, current_date, (frame.shape[1]-345, frame.shape[0]-10), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 255, 0), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break continue # grab the frame dimensions and convert it to a blob (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize( frame, (300, 300)), 0.007843, (300, 300), 127.5) # pass the blob through the network and obtain the detections and # predictions net.setInput(blob) detections = net.forward() rects = [] # To keep the count of objects detected for a target class class_counter = {} # loop over the amount of detected objects for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is # greater than the minimum confidence if confidence > ARGUMENTS["confidence"]: # extract the index of the class label from the # `detections`, then compute the (x, y)-coordinates of # the bounding box for the object idx = int(detections[0, 0, i, 1]) class_name = CLASSES[idx] if class_name not in targets: continue # Increment the amount of objects seen for this class class_counter[class_name] = class_counter.get( class_name, 0) + 1 # Get the bounding box coordinates box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (x1, y1, x2, y2) = box.astype("int") bbox_height = y2 - y1 p1 = (x1, int(y1+bbox_height/2) + 40) p2 = (x2, int(y2-bbox_height/2) + 40) label = "{}: {:.2f}%".format(class_name, confidence * 100) labely = y1 - 15 if y1 - 15 > 15 else y1 + 15 rects.append( [x1, y1, x2, y2, (p1, p2, label, labely, COLORS[idx], class_name)]) objects = centroid_tracker.update(rects) # For each detected object, draw its bounding box, info, centroid and ID # if it meets the specified conditions (if any) for (object_id, (centroid, rect)) in objects.items(): (x1, y1, x2, y2, (p1, p2, label, labely, color, target_name)) = rect for target in program_data['targets'][target_name]: minimum = target.get('min') maximum = target.get('max') detected_objects = class_counter.get(target_name) properties = target.get('properties') # Name of the counter specified by the user in the DSL program counter_name = target.get('counter') if detected_objects is None: continue class_counter[target_name] -= 1 if targets_conditions is not None: # Check if some condition holds true check_targets_conditions( targets_conditions, counters, frame) object_data = { 'bounding_box': (x1, y1, x2, y2), 'middle_line_coords': (p1, p2), 'color': color, 'label': label, 'labely': labely, 'object_id': object_id, 'centroid': centroid, 'counter_name': counter_name, } # Specified both minimum and maximum amount of objects if minimum is not None and maximum is not None: if minimum <= detected_objects <= maximum: check_target_properties( frame, properties, counters, object_data) # Just minimum elif minimum is not None and maximum is None: if detected_objects >= minimum: check_target_properties( frame, properties, counters, object_data) # Just maximum elif minimum is None and maximum is not None: if detected_objects <= maximum: check_target_properties( frame, properties, counters, object_data) # Neither minimum nor maximum else: # So just check properties onwards check_target_properties( frame, properties, counters, object_data) # show the current date on the bottom right corner current_date = datetime.datetime.now().strftime(CURRENT_DATE_FORMAT_STRING) cv2.putText(frame, current_date, (frame.shape[1]-345, frame.shape[0]-10), cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 255, 0), 2) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() video_source.stop()
class MainUi(DetectUi): def __init__(self): super().__init__() self.tic = time.time() # 目标检测/跟踪标志位 # True--> 检测 # False--> 跟踪 self.dect_trac = DETECTORTRACK # initialize the bounding box coordinates of the object we are going to # track self.initBB = None # 选择 tracker self.tracker = OPENCV_OBJECT_TRACKERS["csrt"]() # message 发送标志 self.msg_count = 0 # FPS self.fps = None # 构建神经网络对象 self.yolov3 = GeneralYolov3(YOLOV3PATH, is_tiny=TINYORNOT) # 构建视频对象 self.video = VideoProcess(cv2.VideoCapture(0)) time.sleep(1) # 构建定时器 self.timer = QTimer(self) # 设定定时器溢出时间 self.timer.start(33) # 定时器溢出时发送信号 self.timer.timeout.connect(self.load_video) # 开始加载视频 self.load_video() def load_video(self): """ 视频加载函数 :param : None :return : None """ self.msg_count += 1 self.video.capture_next_frame() self.tic = time.time() if MODIFY: # YOLOV3 + Tracker self.pedestrian_detect_track() else: # YOLOV3 self.pedestrian_detect_yolo() self.videoFrame.setPixmap(self.video.convert_frame()) self.videoFrame.setScaledContents(True) print("%.4f" % (time.time() - self.tic)) ############################################################## # TODO: YOLO 检测 # # 2019/3/29 # ############################################################## def pedestrian_detect_yolo(self): """ 使用 YOLO v3 进行行人识别 :return: """ results, res_find = self.predict() if res_find: label = "正在跟踪目标...\n坐标:" # self.draw_speed() coordinates = self.processing_bounding_box(results) res_coordinate = self.compute_center(coordinates) else: label = "没有找到目标,正在重新寻找..." res_coordinate = None self.send_msg(finded=res_find, coordinate=res_coordinate, text=label) def predict(self): # 创建网络输入的 4D blob. # blobFromImage()将对图像进行预处理 # - 平均减法(用于帮助对抗数据集中输入图像的光照变化) # - 按某种因素进行缩放 blob = cv2.dnn.blobFromImage( self.video.currentFrame.copy(), 1.0 / 255, (self.yolov3.net_width, self.yolov3.net_height), [0, 0, 0], swapRB=False, crop=False) # 设置模型的输入 blob self.yolov3.yolov3_model.setInput(blob) # 前向计算 outputs = self.yolov3.yolov3_model.forward(self.yolov3.outputs_names) # 后处理 results = self.yolov3.postprocess(self.video.currentFrame.copy(), outputs) return results, len(results) def processing_bounding_box(self, results): """ 处理边界框 left,top------------- | | | | | | | | | | | | ---------right,bottom :param results: :return: coordinate : 边界框中心坐标 """ coordinate = None coord_temp = {} for result in results: left, top, right, bottom = result["box"] cv2.rectangle(self.video.currentFrame, (left, top), (right, bottom), (255, 178, 50), 3) # coord_temp["class_id"] = results["class_id"] coord_temp["coordinate"] = (left, top, right, bottom) coordinate = coord_temp["coordinate"] return coordinate def draw_speed(self): """ 计算速率信息 # getPerfProfile() 函数返回模型的推断总时间以及 每一网络层的耗时(in layersTimes) :return: None """ t, _ = self.yolov3.yolov3_model.getPerfProfile() label = 'Inference time: %.2f ms' % \ (t * 1000.0 / cv2.getTickFrequency()) cv2.putText(self.video.currentFrame, label, (10, 465), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (95, 255, 127)) def send_msg(self, **kwargs): """ 向文本浏览框发送信息 :param results: :return: """ if (self.msg_count % 20 == 0) and kwargs["finded"]: self.msg_count = 0 self.textBrowser.append( "%s %s,%s" % (kwargs["text"], kwargs["coordinate"][0][0], kwargs["coordinate"][0][1])) elif self.msg_count % 20 == 0: self.msg_count = 0 self.textBrowser.append("%s" % kwargs["text"]) def compute_center(self, coordinates): """ 计算中心坐标 :param coordinates: tuple 类型 (left, top, right, bottum) :return: """ res_coord = [] if len(coordinates): # for ind in coordinates: # # 计算中心坐标 # center_H = int( # (ind["coordinate"][0] + ind["coordinate"][2]) / 2) # center_V = int( # (ind["coordinate"][1] + ind["coordinate"][3]) / 2) center_H = int((coordinates[0] + coordinates[2]) / 2) center_V = int((coordinates[1] + coordinates[3]) / 2) res_coord.append((center_H, center_V)) print(res_coord[0]) cv2.circle(self.video.currentFrame, (center_H, center_V), 10, (255, 0, 0), -5) return res_coord ############################################################## # END # ############################################################## ############################################################## # TODO: YOLO 检测 + Tracker # # 2019/4/14 # ############################################################## def pedestrian_detect_track(self): if self.dect_trac: if self.msg_count == 0: pass # 加载第一帧 else: # 检测 results, res_find = self.predict() if res_find: # 如果检测到目标 self.dect_trac = False self.initBB = self.processing_bounding_box(results) # self.initBB = (coordinates[0]["coordinate"]) self.tracker.init(self.video.currentFrame.copy(), self.initBB) self.fps = FPS().start() else: self.send_msg(finded=False, text="没有找到目标,正在重新寻找...") else: # 跟踪 if self.initBB is not None: # 捕捉目标的新边界坐标 (success, box) = self.tracker.update(self.video.currentFrame) # 查看是否捕捉成功 if success: (x, y, w, h) = [int(v) for v in box] cv2.rectangle(self.video.currentFrame, (x, y), (x + w, y + h), (0, 255, 0), 2) res_coordinate = self.compute_center((x, y, x + w, y + h)) self.send_msg(finded=True, coordinate=res_coordinate, text="正在跟踪目标...\n坐标:") # 更新FPS self.fps.update() self.fps.stop()
def analyse(station_id, cam_area, cam_name, cam_ip): print("New process started") global RTSP_URL RTSP_URL = cam_ip # connect to client, define collection names client = MongoClient('localhost', 27017) db = client.vahanaDB base = '_data_' + ''.join(cam_area.split()).lower() + '_' + ''.join( cam_name.split()).lower() base_v = '_video_' + ''.join(cam_area.split()).lower() + '_' + ''.join( cam_name.split()).lower() avg_coll_name = station_id + base + '_average' inst_coll_name = station_id + base + '_instant' img_coll_name = station_id + base_v # create collections, access if already created # image collection image_collection = None try: image_collection = db.create_collection(name=station_id, capped=True, size=20212880, max=3) print("Image collection created: ", image_collection.name) except Exception as err: # collection already exists if "already exists" in err: image_collection = db[img_coll_name] print("Image collection already exists") else: print("Image create_collection() ERROR:", err) # instantaneous data collection inst_collection = None try: inst_collection = db.create_collection(name=inst_coll_name, capped=True, size=5242880, max=1) print("Instant collection created: ", inst_collection.name) except Exception as err: # collection already exists if "already exists" in err: inst_collection = db[inst_coll_name] print("Instant collection already exists") else: print("Instant create_collection() ERROR:", err) # time averaged data collection avg_collection = None try: avg_collection = db.create_collection(name=avg_coll_name, capped=True, size=5242880, max=1) print("Average collection created: ", avg_collection.name) except Exception as err: # collection already exists if "already exists" in err: avg_collection = db[avg_coll_name] print("Average collection already exists") else: print("Average create_collection() ERROR:", err) # start analysing video stream frame wise and populate above collections vs = cv2.VideoCapture(RTSP_URL) time.sleep(2.0) fps = FPS().start() # writer = None (W, H) = (None, None) cnt = 0 color = (255, 0, 0) is_processing_flag = False # loop over frames from the video file stream while True: if is_processing_flag: return is_processing_flag = True cnt += 1 # read the next frame from the file (grabbed, frame) = vs.read() # if the frame was not grabbed, then we have reached the end # of the stream if not grabbed: break # if the frame dimensions are empty, grab them if W is None or H is None: (H, W) = frame.shape[:2] (mask_violation, violation_pairs) = process_image(frame, cnt) violation_pairs # Total social distancing violation pairs mask_violation # Total mask violation count encoded_string = base64.b64encode(frame) # print(encoded_string) abc = image_collection.insert({ "station_id": station_id, "cam_ip": cam_ip, "cam_name": cam_name, "cam_area": cam_area, "image": encoded_string }) # print(abc) # show the output frame # cv2.imshow("Frame", cv2.resize(frame, (800, 600))) key = cv2.waitKey(1) & 0xFF # print ("key", key) # if the `q` key was pressed, break from the loop if key == ord("q"): break # update the FPS counter fps.update() print("Count is : ", cnt) is_processing_flag = False # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() # release the file pointers print("[INFO] cleaning up...") # writer.release() vs.release() # insert random non img documents (meant to be done inside previous while loop, using model # inferences to populate the document) if inst_collection is not None: inst_data_doc = { "tiles": [ { "title": "Crowd Density", "value": 7, }, { "title": "Social Distance violations", "value": 5, }, { "title": "Mask violations", "value": 2, }, ] } insert_id = inst_collection.insert_one(inst_data_doc).inserted_id print("Instant document inserted with IDs: ", insert_id) if avg_collection is not None: avg_data_doc = { "tiles": [ { "title": "Average Crowd Density", "value": 2, }, { "title": "Average Social Distance violations", "value": 3, }, { "title": "Average Mask violations", "value": 4, }, ] } insert_id = avg_collection.insert_one(avg_data_doc).inserted_id print("Average document inserted with ID: ", insert_id)
def main(): num_cores = 4 Pool(num_cores) EYE_AR_THRESH = 0.23 EYE_AR_CONSEC_FRAMES = 3 # initialize the frame counters and the total number of blinks COUNTER = 0 TOTAL = 0 # initialize timer TIMER = 0 cycle = 60 # default cycle is 1 minute # initialize break flag bflag = 0 # open sound pygame.mixer.init() bang = pygame.mixer.Sound("alarm/sounds_warning.wav") # initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor print("[INFO] loading facial landmark predictor...") detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') # grab the indexes of the facial landmarks for the left and # right eye, respectively (lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"] # start the video stream thread print("[INFO] starting video stream thread...") vs = VideoStream(src=0).start() time.sleep(1.0) sense.clear() prev = time.time() fps = FPS().start() # loop over frames from the video stream while True: # grab the frame from the threaded video file stream, resize # it, and convert it to grayscale # channels) frame = vs.read() frame = imutils.resize(frame, width=300) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # detect faces in the grayscale frame rects = detector(gray, 0) # joystick for event in sense.stick.get_events(): sense.clear() # Check if the joystick was pressed if event.action == "pressed": if event.direction == "up": if cycle <= 480: cycle += 120 show_number(cycle / 60, 0, 0, 255) elif event.direction == "down": if cycle >= 240: cycle -= 120 show_number(cycle / 60, 0, 0, 255) elif event.direction == "left": if cycle >= 120: cycle -= 60 show_number(cycle / 60, 0, 0, 255) elif event.direction == "right": if cycle <= 540: cycle += 60 show_number(cycle / 60, 0, 0, 255) elif event.direction == "middle": bflag = 1 # Wait a while and then clear the screen time.sleep(0.4) sense.clear() # for timer cur = time.time() if cur - prev >= 1: prev = cur TIMER = TIMER + 1 if TIMER != 0 and TIMER % cycle == 0: # change to cycle if TOTAL < cycle * 0.2: # change to (cycle / 60) * 12 = cycle * 0.2 bang.play() TOTAL = 0 # loop over the face detections for rect in rects: # for timer cur = time.time() if cur - prev >= 1: prev = cur TIMER = TIMER + 1 # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # arrayc shape = predictor(gray, rect) shape = face_utils.shape_to_np(shape) # extract the left and right eye coordinates, then use the # coordinates to compute the eye aspect ratio for both eyes leftEye = shape[lStart:lEnd] rightEye = shape[rStart:rEnd] leftEAR = eye_aspect_ratio(leftEye) rightEAR = eye_aspect_ratio(rightEye) # average the eye aspect ratio together for both eyes ear = (leftEAR + rightEAR) / 2.0 # compute the convex hull for the left and right eye, then # visualize each of the eyes leftEyeHull = cv2.convexHull(leftEye) rightEyeHull = cv2.convexHull(rightEye) cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1) cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1) # check to see if the eye aspect ratio is below the blink # threshold, and if so, increment the blink frame counter if ear < EYE_AR_THRESH: COUNTER += 1 # otherwise, the eye aspect ratio is not below the blink # threshold else: # if the eyes were closed for a sufficient number of # then increment the total number of blinks if COUNTER >= EYE_AR_CONSEC_FRAMES: TOTAL += 1 # reset the eye frame counter COUNTER = 0 # draw the total number of blinks on the frame along with # the computed eye aspect ratio for the frame #cv2.putText(frame, "Blinks: {}".format(TOTAL), (10, 30), # cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) #cv2.putText(frame, "EAR: {:.2f}".format(ear), (210, 30), # cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) fps.update() # show digit show_number(TOTAL % 100, 0, 80, 0) # show fps fps.stop() #cv2.putText(frame, "FPS: {:.2f}".format(fps.fps()), (210, 90), # cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # show the frame #cv2.imshow("Frame", frame) #key = cv2.waitKey(1) & 0xFF print(TIMER) print("{:.2f}".format(fps.fps())) # if the `q` key was pressed, break from the loop if bflag: break # do a bit of cleanup cv2.destroyAllWindows() vs.stop() sense.clear()
def do_GET(self): if self.path == '/live.mjpg': self.send_response(200) self.send_header('Age', 0) self.send_header('Cache-Control', 'no-cache, private') self.send_header('Pragma', 'no-cache') self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME') self.end_headers() try: while True: with output.condition: output.condition.wait() fps = FPS().start() frame = output.frame data = np.fromstring(frame, dtype=np.uint8) frame = cv2.imdecode(data, 1) frame = imutils.resize(frame, width=640) (fH, fW) = frame.shape[:2] # if the input queue *is* empty, give the current frame to # classify if inputQueue.empty(): inputQueue.put(frame) # if the output queue *is not* empty, grab the detections if not outputQueue.empty(): global detections detections = outputQueue.get() # check to see if our detectios are not None (and if so, we'll # draw the detections on the frame) if detections is not None: # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated # with the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` # is greater than the minimum confidence if confidence < args["confidence"]: continue # otherwise, extract the index of the class label from # the `detections`, then compute the (x, y)-coordinates # of the bounding box for the object idx = int(detections[0, 0, i, 1]) dims = np.array([fW, fH, fW, fH]) box = detections[0, 0, i, 3:7] * dims (startX, startY, endX, endY) = box.astype("int") # draw the prediction on the frame label = "{}: {:.2f}%".format( CLASSES[idx], confidence * 100) cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) # show the output frame # update the FPS counter fps.update() fps.stop() # stop the timer and display FPS information r, frame = cv2.imencode(".jpg", frame) self.wfile.write(b'--FRAME\r\n') self.send_header('Content-Type', 'image/jpeg') self.send_header('Content-Length', len(frame)) self.end_headers() self.wfile.write(frame) self.wfile.write(b'\r\n') except Exception as e: logging.warning('Removed streaming client %s: %s', self.client_address, str(e)) else: self.send_error(404) self.end_headers()
# created a *threaded *video stream, allow the camera sensor to warmup, # and start the FPS counter print("[INFO] sampling frames from PiVideoStream module...") vs = PiVideoStream().start() time.sleep(2.0) fps = FPS().start() # loop over some frames...this time using the threaded stream while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=400) # update the FPS counter fps.update() cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break except KeyboardInterrupt: # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
class DroidVisionThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.running = True self.fps_counter = FPS().start() self.camera = PiVideoStream(resolution=(config.RAW_FRAME_WIDTH, config.RAW_FRAME_HEIGHT)) self.camera.start() time.sleep(config.CAMERA_WARMUP_TIME) # wait for camera to initialise self.frame = None self.frame_chroma = None self.centre = config.CENTRE self.can_see_lines = False self.last_yellow_mean = config.WIDTH * 0.8 self.last_blue_mean = config.WIDTH * 0.2 self.desired_steering = config.NEUTRAL_STEERING # 0 = left, 0.5 = center, 1 = right self.desired_throttle = config.NEUTRAL_THROTTLE # 0 = stop, 0.5 = medium speed, 1 = fastest def run(self): debug("DroidVisionThread: Thread started") self.vision_processing() self.camera.stop() cv2.destroyAllWindows() debug("DroidVisionThread: Thread stopped") def stop(self): self.running = False debug("DroidVisionThread: Stopping thread") def vision_processing(self): while self.running: self.grab_frame() # colour mask blue_mask = cv2.inRange(self.frame_chroma, config.BLUE_CHROMA_LOW, config.BLUE_CHROMA_HIGH) yellow_mask = cv2.inRange(self.frame_chroma, config.YELLOW_CHROMA_LOW, config.YELLOW_CHROMA_HIGH) colour_mask = cv2.bitwise_or(blue_mask, yellow_mask) colour_mask = cv2.erode(colour_mask, config.ERODE_KERNEL) colour_mask = cv2.dilate(colour_mask, config.DILATE_KERNEL) # lines lines = cv2.HoughLinesP(colour_mask, config.HOUGH_LIN_RES, config.HOUGH_ROT_RES, config.HOUGH_VOTES, config.HOUGH_MIN_LEN, config.HOUGH_MAX_GAP) blue_lines = np.array([]) yellow_lines = np.array([]) if lines != None: for line in lines: x1,y1,x2,y2 = line[0] angle = np.rad2deg(np.arctan2(y2-y1, x2-x1)) if config.MIN_LINE_ANGLE < abs(angle) < config.MAX_LINE_ANGLE: if config.IMSHOW: cv2.line(self.frame, (x1,y1), (x2,y2), (0,0,255), 1) if angle > 0: yellow_lines = np.append(yellow_lines, [x1, x2]) elif angle < 0: blue_lines = np.append(blue_lines, [x1, x2]) # find centre blue_mean = self.last_blue_mean yellow_mean = self.last_yellow_mean if len(blue_lines): blue_mean = int(np.mean(blue_lines)) if len(yellow_lines): yellow_mean = int(np.mean(yellow_lines)) self.centre = (blue_mean + yellow_mean) / 2.0 self.last_blue_mean = blue_mean self.last_yellow_mean = yellow_mean self.can_see_lines = (len(blue_lines) or len(yellow_lines)) # set steering and throttle self.desired_steering = (1.0 - (self.centre / config.WIDTH)) if len(blue_lines) or len(yellow_lines): self.desired_throttle = 0.22 else: self.desired_throttle = 0 if config.IMSHOW: cv2.circle(self.frame, (int(self.centre), config.HEIGHT - 20), 10, (0,0,255), -1) cv2.imshow("colour_mask without noise", colour_mask) cv2.imshow("raw frame", self.frame) cv2.waitKey(1) def grab_frame(self): self.fps_counter.update() # grab latest frame and index the ROI self.frame = self.camera.read() self.frame = self.frame[config.ROI_YMIN:config.ROI_YMAX, config.ROI_XMIN:config.ROI_XMAX] # convert to chromaticity colourspace B = self.frame[:, :, 0].astype(np.uint16) G = self.frame[:, :, 1].astype(np.uint16) R = self.frame[:, :, 2].astype(np.uint16) Y = 255.0 / (B + G + R) b = (B * Y).astype(np.uint8) g = (G * Y).astype(np.uint8) r = (R * Y).astype(np.uint8) self.frame_chroma = cv2.merge((b,g,r)) def get_fps(self): self.fps_counter.stop() return self.fps_counter.fps() def get_error(self): return (config.CENTRE - self.centre) def get_steering_throttle(self): return self.desired_steering, self.desired_throttle
def getRealTimePeopleCount(): try: CLASSES = ["person"] # Grab path to current working directory CWD_PATH = os.getcwd() OUTPUT_DIRECTORY = 'output' MODEL_DIRECTORY = 'inference_graph' PATH_TO_CKPT = os.path.join(CWD_PATH, MODEL_DIRECTORY, 'frozen_inference_graph.pb') output = os.path.join(CWD_PATH, OUTPUT_DIRECTORY, 'example_01.avi') input = os.path.join(CWD_PATH, 'videos', 'example_01.avi') input = None defaultConfidence = 0.4 detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') sess = tf.Session(graph=detection_graph) print("[INFO] loading model...") # if a video path was not supplied, grab a reference to the webcam if input is None: print("[INFO] starting video stream...") vs = VideoStream( src='rtsp://*****:*****@[email protected]').start() time.sleep(2.0) # otherwise, grab a reference to the video file else: print("[INFO] opening video file...") vs = cv2.VideoCapture(input) # initialize the video writer (we'll instantiate later if need be) writer = None # initialize the frame dimensions (we'll set them as soon as we read # the first frame from the video) W = None H = None # instantiate our centroid tracker, then initialize a list to store # each of our dlib correlation trackers, followed by a dictionary to # map each unique object ID to a TrackableObject ct = CentroidTracker(maxDisappeared=40, maxDistance=50) trackers = [] trackableObjects = {} # initialize the total number of frames processed thus far, along # with the total number of objects that have moved either up or down totalFrames = 0 peopleCount = 0 # start the frames per second throughput estimator fps = FPS().start() # loop over frames from the video stream while True: # grab the next frame and handle if we are reading from either # VideoCapture or VideoStream frame = vs.read() frame = frame[1] if input is not None else frame # if we are viewing a video and we did not grab a frame then we # have reached the end of the video if input is not None and frame is None: break # resize the frame to have a maximum width of 500 pixels (the # less data we have, the faster we can process it), then convert # the frame from BGR to RGB for dlib frame = imutils.resize(frame, width=500) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # if the frame dimensions are empty, set them if W is None or H is None: (H, W) = frame.shape[:2] # if we are supposed to be writing a video to disk, initialize # the writer if output is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(output, fourcc, 30, (W, H), True) # initialize the current status along with our list of bounding # box rectangles returned by either (1) our object detector or # (2) the correlation trackers rects = [] # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % 30 == 0: trackers = [] rows = frame.shape[0] cols = frame.shape[1] inp = cv2.resize(frame, (300, 300)) inp = inp[:, :, [2, 1, 0]] # BGR2RGB out = sess.run( [ sess.graph.get_tensor_by_name('num_detections:0'), sess.graph.get_tensor_by_name('detection_scores:0'), sess.graph.get_tensor_by_name('detection_boxes:0'), sess.graph.get_tensor_by_name('detection_classes:0') ], feed_dict={ 'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3) }) # Visualize detected bounding boxes. num_detections = int(out[0][0]) for i in range(num_detections): classId = int(out[3][0][i]) classId -= 1 score = float(out[1][0][i]) bbox = [float(v) for v in out[2][0][i]] if score > defaultConfidence: startX = int(bbox[1] * cols) startY = int(bbox[0] * rows) endX = int(bbox[3] * cols) endY = int(bbox[2] * rows) tracker = dlib.correlation_tracker() rect = dlib.rectangle(startX, startY, endX, endY) tracker.start_track(rgb, rect) # add the tracker to our list of trackers so we can # utilize it during skip frames trackers.append(tracker) else: # loop over the trackers for tracker in trackers: # set the status of our system to be 'tracking' rather # than 'waiting' or 'detecting' status = "Tracking" # update the tracker and grab the updated position tracker.update(rgb) pos = tracker.get_position() # unpack the position object startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) # add the bounding box coordinates to the rectangles list rects.append((startX, startY, endX, endY)) objects = ct.update(rects) # loop over the tracked objects for (objectID, centroid) in objects.items(): # check to see if a trackable object exists for the current # object ID to = trackableObjects.get(objectID, None) # if there is no existing trackable object, create one if to is None: to = TrackableObject(objectID, centroid) # otherwise, there is a trackable object so we can utilize it # to determine direction else: # the difference between the y-coordinate of the *current* # centroid and the mean of *previous* centroids will tell # us in which direction the object is moving (negative for # 'up' and positive for 'down') y = [c[1] for c in to.centroids] direction = centroid[1] - np.mean(y) to.centroids.append(centroid) # check to see if the object has been counted or not if not to.counted: # if the direction is negative (indicating the object # is moving up) AND the centroid is above the center # line, count the object peopleCount += 1 to.counted = True # if the direction is positive (indicating the object # is moving down) AND the centroid is below the # center line, count the object # store the trackable object in our dictionary trackableObjects[objectID] = to # draw both the ID of the object and the centroid of the # object on the output frame text = "ID {}".format(objectID) cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.rectangle(frame, ((centroid[0] - 30, centroid[1] - 40)), ((centroid[0] + 30, centroid[1] + 40)), (0, 255, 0), 1) # construct a tuple of information we will be displaying on the # frame info = [("PeopleCount", peopleCount)] # loop over the info tuples and draw them on our frame for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # check to see if we should write the frame to disk if writer is not None: writer.write(frame) # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # increment the total number of frames processed thus far and # then update the FPS counter totalFrames += 1 fps.update() # stop the timer and display FPS information fps.stop() outputVideoUrl = "localhost:8000/static/videos/example_01.avi" print("PeopleCount", peopleCount) print("outputVideoUrl", outputVideoUrl) print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # check to see if we need to release the video writer pointer if writer is not None: writer.release() # if we are not using a video file, stop the camera video stream if input is None: vs.stop() # otherwise, release the video file pointer else: vs.release() # close any open windows cv2.destroyAllWindows() return (peopleCount, outputVideoUrl) except Exception as ex: traceback.print_exc() raise ex
def lock(): RELAY = 17 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(RELAY, GPIO.OUT) GPIO.output(RELAY, GPIO.LOW) # Initialize 'currentname' to trigger only when a new person is identified. currentname = "unknown" # Determine faces from encodings.pickle file model created from train_model.py encodingsP = "encodings.pickle" # use this xml file # https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml cascade = "haarcascade_frontalface_default.xml" # load the known faces and embeddings along with OpenCV's Haar # cascade for face detection print("[INFO] loading encodings + face detector...") data = pickle.loads(open(encodingsP, "rb").read()) detector = cv2.CascadeClassifier(cascade) # initialize the video stream and allow the camera sensor to warm up print("[INFO] starting video stream...") vs = VideoStream(src=0).start() #vs = VideoStream(usePiCamera=True).start() time.sleep(2.0) # start the FPS counter fps = FPS().start() prevTime = 0 doorUnlock = False # loop over frames from the video file stream while True: # grab the frame from the threaded video stream and resize it # to 500px (to speedup processing) frame = vs.read() frame = imutils.resize(frame, width=500) # convert the input frame from (1) BGR to grayscale (for face # detection) and (2) from BGR to RGB (for face recognition) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # detect faces in the grayscale frame rects = detector.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE) # OpenCV returns bounding box coordinates in (x, y, w, h) order # but we need them in (top, right, bottom, left) order, so we # need to do a bit of reordering boxes = [(y, x + w, y + h, x) for (x, y, w, h) in rects] # compute the facial embeddings for each face bounding box encodings = face_recognition.face_encodings(rgb, boxes) names = [] # loop over the facial embeddings for encoding in encodings: # attempt to match each face in the input image to our known # encodings matches = face_recognition.compare_faces(data["encodings"], encoding) name = "Unknown" # if face is not recognized, then print Unknown # check to see if we have found a match if True in matches: # find the indexes of all matched faces then initialize a # dictionary to count the total number of times each face # was matched matchedIdxs = [i for (i, b) in enumerate(matches) if b] counts = {} # to unlock the door GPIO.output(RELAY, GPIO.HIGH) prevTime = time.time() doorUnlock = True print("door unlock") # loop over the matched indexes and maintain a count for # each recognized face face for i in matchedIdxs: name = data["names"][i] counts[name] = counts.get(name, 0) + 1 # determine the recognized face with the largest number # of votes (note: in the event of an unlikely tie Python # will select first entry in the dictionary) name = max(counts, key=counts.get) # If someone in your dataset is identified, print their name on the screen if currentname != name: currentname = name print(currentname) # update the list of names names.append(name) # lock the door after 5 seconds if doorUnlock == True and time.time() - prevTime > 5: doorUnlock = False GPIO.output(RELAY, GPIO.LOW) print("door lock") # loop over the recognized faces for ((top, right, bottom, left), name) in zip(boxes, names): # draw the predicted face name on the image - color is in BGR cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) y = top - 15 if top - 15 > 15 else top + 15 cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX, .8, (255, 0, 0), 2) # display the image to our screen cv2.imshow("Facial Recognition is Running", frame) key = cv2.waitKey(1) & 0xFF # quit when 'esc' key is pressed #k = cv2.waitKey(1) if key == 27: break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
def track(detection_model, class_set, input_vedio=None, output_vedio=None, frame_width=400, skip_frames=2, confidence=0.4, verbose=True): if input_vedio is None: print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) else: print("[INFO] opening video file...") vs = cv2.VideoCapture(input_vedio) W = None H = None writer = None fps = None track_id = None totalFrames = 0 display_str = 'waiting' mode = WAIT multi_tracker = SimpleMultiTracker(debug=verbose) # loop over frames from the video stream while True: # grab the next frame and handle if we are reading from either # VideoCapture or VideoStream frame = vs.read() frame = frame[1] if input_vedio is not None else frame # if we are viewing a video and we did not grab a frame then we # have reached the end of the video if input_vedio is not None and frame is None: break # resize the frame to have a maximum width of frame_width frame = imutils.resize(frame, width=frame_width) # if the frame dimensions are empty, set them if W is None or H is None: (H, W) = frame.shape[:2] # detection_model.performDetect(None, initOnly=True) # if we are supposed to be writing a video to disk, initialize # the writer if output_vedio is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MP4V") writer = cv2.VideoWriter(output_vedio, fourcc, 30, (W, H), True) if mode != WAIT: # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % skip_frames == 0: raw_boxes, scores, classes = detection_model.detect(frame) # print(classes) # loop over the detections boxes = [] for i in range(len(scores)): if scores[i] > confidence: if classes[i] in class_set: boxes.append(raw_boxes[i]) if totalFrames == 0: rects = multi_tracker.init_tracker(frame, boxes) else: rects = multi_tracker.update_tracker(frame, boxes) if mode == TRACKING: if multi_tracker.tracked[track_id]: # if tracked after detection, init a new single tracker multi_tracker.init_single_tracker(track_id, frame) else: multi_tracker.update_single_tracker(frame) # otherwise, we should utilize our object *trackers* rather than # object *detectors* to obtain a higher frame processing throughput else: if mode == TRACKING: multi_tracker.update_single_tracker(frame) rects = multi_tracker.appearing_boxes() if rects is not None: for rect in rects.values(): (startX, startY, endX, endY) = rect cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), 2) # loop over the tracked objects for (objectID, box) in rects.items(): (startX, startY, endX, endY) = box cX = int((startX + endX) / 2.0) cY = int((startY + endY) / 2.0) centroid = (cX, cY) # draw both the ID of the object and the centroid of the # object on the output frame text = "ID {}".format(objectID) cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.circle(frame, (centroid[0], centroid[1]), 2, (0, 0, 255), -1) if totalFrames == 0: cv2.imshow("Frame", frame) # selected_IDs.append(pop_up_box()) fps = FPS().start() # increment the total number of frames processed thus far and # then update the FPS counter totalFrames += 1 fps.update() if verbose: multi_tracker.show_info('Frame{}'.format(totalFrames)) if writer is not None: writer.write(frame) cv2.putText(frame, display_str, (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2) cv2.imshow("Frame", frame) if mode == WAIT: key = cv2.waitKey(33) & 0xFF else: key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break elif key == ord("d"): mode = DETECTING display_str = 'DETECTING' elif key == 13: # enter key if mode == DETECTING: mode = INPUTTING display_str = 'INPUTTING:' elif mode == INPUTTING: track_id = int(display_str.split(':')[-1]) mode = TRACKING display_str = 'TRACKING:{}'.format(track_id) multi_tracker.init_single_tracker(track_id, frame) elif ord('0') <= key <= ord('9') and mode == INPUTTING: num = key - ord('0') display_str += str(num) else: if key != 255: print('unused key:{}'.format(key)) # stop the timer and display FPS information if fps is not None: fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # check to see if we need to release the video writer pointer if writer is not None: writer.release() # if we are not using a video file, stop the camera video stream if input_vedio is None: vs.stop() # otherwise, release the video file pointer else: vs.release() # close any open windows cv2.destroyAllWindows()
def face_detection(): # Load Tensorflow model detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') sess = tf.Session(graph=detection_graph) image_tensor = detection_graph.get_tensor_by_name('image_tensor:0') # Each box represents a part of the image where a particular object was detected. detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0') # Each score represent how level of confidence for each of the objects. # Score is shown on the result image, together with the class label. detection_scores = detection_graph.get_tensor_by_name('detection_scores:0') # Actual detection. detection_classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name('num_detections:0') # Start video stream cap = WebcamVideoStream(0).start() fps = FPS().start() while True: frame = cap.read() # Expand dimensions since the model expects images to have shape: [1, None, None, 3] expanded_frame = np.expand_dims(frame, axis=0) (boxes, scores, classes, num_c) = sess.run([ detection_boxes, detection_scores, detection_classes, num_detections ], feed_dict={image_tensor: expanded_frame}) # Visualization of the detection vis_util.visualize_boxes_and_labels_on_image_array( frame, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates=True, line_thickness=2, min_score_thresh=0.40) cv2.imshow('Detection', frame) fps.update() if cv2.waitKey(1) == ord('q'): fps.stop() break print("Fps: {:.2f}".format(fps.fps())) fps.update() cap.stop() cv2.destroyAllWindows()
def thrededed(): # created a *threaded* video stream, allow the camera sensor to warmup, # and start the FPS counter print("[INFO] sampling THREADED frames from webcam...") if args["cameravideo"] == 0: src = 0 else: src = input("input video") assert os.path.exists(src), "I did not find the file at, " + str(src) vs = WebcamVideoStream(src=src).start() fps = FPS().start() # multithreded # loop over some frames...this time using the threaded stream num_frame = fps._numFrames < args["num_frames"] if args["infinite"] == True: if args["num_frames"] > 0: sys.exit( """You said infinite and limited frame. it is not sensible """) num_frame = True while num_frame: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels # with lock: try: # with lock: with lock: grapped, frame = vs.read() if grapped == False: print("camera connection maybe lost or video has ended.") break frame = imutils.resize(frame, width=400) # check to see if the frame should be displayed to our screen if args["display"] > 0: cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == 27 or key == ord('q'): vs.stop() if vs.stopped: return break except (KeyboardInterrupt, SystemExit): vs.stop() if vs.stopped: return sys.exit("Program Interrupted") # Thread.daemon = False except AssertionError: vs.stop() print("AssertionError Error.") if vs.stopped: return except TypeError: print("There is no lane") time.sleep(.5) continue # pass #çizgi algılanamazsa çökmesin. except Exception as e: print(e) vs.stop() if vs.stopped: return # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
def process_face_detection(encoding_file, face_cascade): """ ************************************ Inside process_face_detection function **************************************** This is the main function which will detect the faces from a real time video. This function is the primary function that will first load all the data from the encoding file, and later this will open the camera and create co-ordinate of each faces in the camera. If any newly created co-ordinate matches with the co-ordinate present in the encoding file, then it will create a square box and name of the person over the faces on the camera. But if the function can not able to find the encoding file in the directory, then it will exit from the program. ************************************ Inside process_face_detection function **************************************** """ try: with open(encoding_file, "rb") as file: unpickler = pickle.Unpickler(file) data = unpickler.load() # cap = cv2.VideoCapture(-1, cv2.CAP_DSHOW) - For Windows 10 system cap = cv2.VideoCapture(-1) time.sleep(2.0) capture_duration = 3 start_time = time.time() end_time = 0 name = "" fps = FPS().start() while cap.isOpened(): _, img = cap.read() # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) try: rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) faces = face_cascade.detectMultiScale(rgb, 1.1, 4) boxes = [(y, x + w, y + h, x) for (x, y, w, h) in faces] encodings = face_recognition.face_encodings(rgb, boxes) names = [] if int(end_time) - int(start_time) > capture_duration: print(name) fps.update() fps.stop() cap.release() cv2.destroyAllWindows() exit_program() for encoding in encodings: matches = face_recognition.compare_faces( data["encodings"], encoding) name = "UNKNOWN" if True in matches: matched_id = [i for (i, b) in enumerate(matches) if b] counts = {} for i in matched_id: name = data["names"][i] counts[name] = counts.get(name, 0) + 1 name = max(counts, key=counts.get) names.append(name) for ((top, right, bottom, left), name) in zip(boxes, names): cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2) y = top - 15 if top - 15 > 15 else top + 15 cv2.putText(img, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2) end_time = time.time() cv2.imshow("Image Frame", img) if cv2.waitKey(1) & 0xFF == ord('q'): break fps.update() except cv2.error: print("ERROR - OpenCV RGB") fps.stop() cap.release() cv2.destroyAllWindows() except EOFError: print( "ERROR : EOFError - Pickle File : encodings.pickle, has no data - " "inside process_face_detection function.") exit_program() except FileNotFoundError: print( "ERROR : FileNotFoundError - Pickle File : encodings.pickle, not present - " "inside process_face_detection function.") exit_program()
def main(): global arr global detection cap = cv2.VideoCapture( gstreamer_pipeline(capture_width=cap_width, capture_height=cap_height, flip_method=2), cv2.CAP_GSTREAMER) #cap = cv2.VideoCapture('../video-test2.mp4') if cap.isOpened(): window_handle = cv2.namedWindow("frame", cv2.WINDOW_AUTOSIZE) fps_i = None empty_frame = 0 th = threading.Thread(target=send_picture, args=(None, None, delay)) th.start() while cv2.getWindowProperty("frame", 0) >= 0: fps = FPS().start() ret_val, img = cap.read() frame = img.copy() img, detections = detect(img) #print("# detected : ",len(detections)) for i in range(len(detections)): num, x1, y1, x2, y2 = get_bbox(detections[i]) if num in [3, 4, 6, 8]: detection = detection + 1 crop = frame[y1:y2, x1:x2] results = detect_plate(crop) #print(results) if results['results']: arr = accum_vote(results['results'], arr) cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), 4) if detection >= 10: detection = 0 if not check_thread_alive(th): results, arr = calculate_vote(arr) th = threading.Thread(target=send_picture, args=(frame, results, delay)) cl = threading.Thread(target=clear_arr) th.start() cl.start() else: pass cv2.imshow("frame", img) if fps_i is None: fps_i = 0 fps.update() fps.stop() fps_i = fps.fps() #print("FPS: {:.2f}".format(fps.fps())) keyCode = cv2.waitKey(30) & 0xFF if keyCode == 27: break cap.release() cv2.destroyAllWindows() else: print("Unable to open camera")
def main(classes, proto, model, video, label_input, output, min_confidence): # pre-load detection model print("[INFO] loading detection model...") net = cv2.dnn.readNetFromCaffe(prototxt=proto, caffeModel=model) print('[INFO] Starting video stream...') if not video: # start web-cam feed vs = cv2.VideoCapture(0) else: # start video stream vs = cv2.VideoCapture(video) # initializing variables tracker = None writer = None label = "" fps = FPS().start() # main loop while True: grabbed, frame = vs.read() if frame is None: break # resize the frame & convert to RGB color space (dlib needs RGB) frame = imutils.resize(frame, width=600) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if output is not None and writer is None: # initialize output file writer writer = create_videowriter(output, 30, (frame.shape[1], frame.shape[0])) if tracker is None: # perform object detection on frame height, width = frame.shape[:2] detections = forward_passer(net, frame, timing=False) # if any objects are detected if len(detections) > 0: # choose best detection i = np.argmax(detections[0, 0, :, 2]) confidence = detections[0, 0, i, 2] label = classes[int(detections[0, 0, i, 1])] if confidence > min_confidence and label == label_input: # create tracker tracker, points = create_tracker(detections, width, height, best_detect=i, frame=rgb) # draw rectangle around the tracker and label it cv2.rectangle(frame, (points[0], points[1]), (points[2], points[3]), (0, 255, 0), 2) cv2.putText(frame, label, (points[0], points[1] - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2) else: # update the tracker points = update_tracker(tracker, rgb) # draw rectangle around the tracker and label it cv2.rectangle(frame, (points[0], points[1]), (points[2], points[3]), (0, 255, 0), 2) cv2.putText(frame, label, (points[0], points[1] - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2) if writer is not None: writer.write(frame) # show result cv2.imshow("Tracking", frame) key = cv2.waitKey(1) & 0xFF # quit if 'q' is pressed if key == ord('q'): break fps.update() fps.stop() print(f'[INFO] Elapsed time: {round(fps.elapsed(), 2)}') print(f'[INFO] approximate FPS: {round(fps.fps(), 2)}') # release video writer end-point if writer is not None: writer.release() # release video stream end-point cv2.destroyAllWindows() vs.release()
def recognize(): """ Open picamera and use the trained model to recognize the customer. :return: - username, or label of the recognized person. - 'unknown' if the person is not recognized. """ detector = cv2.dnn.readNetFromCaffe(DEPLOY_PROTOTXT_PATH, RES10_300X300_PATH) recognizer = pickle.loads(open(RECOGNIZER_PATH, "rb").read()) le = pickle.loads(open(LABEL_ENCODER_PATH, "rb").read()) print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) fps = FPS().start() tstart = time.time() name = 'unknown' while True: frame = vs.read() frame = imutils.resize(frame, width=600) (h, w) = frame.shape[:2] imageBlob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) detector.setInput(imageBlob) detections = detector.forward() for i in range(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > CONFIDENCE: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") face = frame[startY:endY, startX:endX] (fH, fW) = face.shape[:2] if fW < 20 or fH < 20: continue coor = [((startY, startX + (endX - startX), startY + (endY - startY), startX))] rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) encodings = face_recognition.face_encodings(rgb_frame, coor) preds = recognizer.predict_proba(encodings)[0] j = np.argmax(preds) proba = preds[j] name = le.classes_[j] text = "{}: {:.2f}%".format(name, proba * 100) y = startY - 10 if startY - 10 > 10 else startY + 10 cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2) cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) fps.update() cv2.imshow("Frame", frame) tcur = time.time() if (tcur - tstart > 15 and name != None and proba > 0.6)\ or (tcur - tstart > 60): break key = cv2.waitKey(1) & 0xFF fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() vs.stop() if name != None: return name return 'unknown'
def main_func(args, vs, yolo): scheduler = BackgroundScheduler() # 初始化任务函数 # 添加调度任务 # 调度方法为 timedTask,触发器选择 interval(间隔性),间隔时长为 1 秒 scheduler.add_job(timedTask, 'interval', seconds=cfg.KAFKA.PUSHINTER) # 间隔为1秒 # 启动调度任务 scheduler.start() writer = None # 写入对象,如果要写入视频,将实例化该对象 W = None H = None # W,H是我们框架的大小、 ct = CentroidTracker( maxDisappeared=cfg.CRT.MAXDISAPPEARED, maxDistance=cfg.CRT.MAXDISTANCE) # 质心追踪对象,连续40帧脱靶则注销#maxt=120 trackers = [] # 用于存储dlib相关追踪器的列表 trackableObjects = {} # 映射id的字典 totalFrames = 0 # 已处理帧总数 global total_up, total_down # 向下运动的人数 fps = FPS().start() # 用于基准测试的每秒帧数估算器 inference_times = [] # 已完成所有初始化,下面遍历传入的帧 while True: # grab the next frame and handle if we are reading from either # VideoCapture or VideoStream st = time.time() frame = vs.read() frame = frame[1] if cfg.DATA.INPUT else frame # if we are viewing a video and we did not grab a frame then we # have reached the end of the video if cfg.DATA.INPUT is not None and frame is None: break # resize the frame to have a maximum width of 500 pixels (the # less data we have, the faster we can process it), then convert # the frame from BGR to RGB for dlib frame = imutils.resize( frame, width=cfg.FRAME.WIDTH) # 调整框架的最大宽度为500像素,拥有的像素越少,处理速度越快 rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) et = time.time() # if the frame dimensions are empty, set them if W is None or H is None: (H, W) = frame.shape[:2] # initialize the current status along with our list of bounding # box rectangles returned by either (1) our object detector or # (2) the correlation trackers status = "Waiting" rects = [] # 保存检测到或追踪到的对象 if totalFrames % 2 == 0: if totalFrames % cfg.FRAME.SKIPFRAMES == 0: # set the status and initialize our new set of object trackers status = "Detecting" trackers_a = [] # 追踪对象的列表 st = time.time() image = Image.fromarray(frame[..., ::-1]) # bgr to rgb boxs, class_names = yolo.detect_image(image) et = time.time() print('detection take time : ', et - st) for box in boxs: box = np.array(box) (minx, miny, maxx, maxy) = box.astype("int") cY = int((miny + maxy) / 2.0) if cY > int(H * cfg.CRT.MINCY) and cY < int( H * cfg.CRT.MAXCY): tracker = dlib.correlation_tracker() # 实例化dlib相关性追踪器 rect = dlib.rectangle( minx, miny, maxx, maxy) # 将对象的边界框坐标传给dlib.rectangle,结果存储在rect中 cv2.rectangle(frame, (minx, miny), (maxx, maxy), (2, 255, 0), 2) rects.append((minx, miny, maxx, maxy)) # 开始追踪 tracker.start_track(rgb, rect) # add the tracker to our list of trackers so we can # utilize it during skip frames trackers_a.append(tracker) else: st = time.time() # loop over the trackers for tracker in trackers_a: # set the status of our system to be 'tracking' rather # than 'waiting' or 'detecting' status = "Tracking" # update the tracker and grab the updated position tracker.update(rgb) pos = tracker.get_position() # unpack the position object startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) cv2.rectangle(frame, (startX, startY), (endX, endY), (2, 0, 255), 2) rects.append((startX, startY, endX, endY)) et = time.time() tt = et - st # 画一条水平的可视化线(行人必须交叉才能被追踪),并使用质心跟踪器更新对象质心 # draw a horizontal line in the center of the frame -- once an # object crosses this line we will determine whether they were cv2.line(frame, (int(W * 0), int(H * cfg.FRAME.LINE)), (int(W * 1), int(H * cfg.FRAME.LINE)), (0, 255, 0), 2) # 闸机测试 # use the centroid tracker to associate the (1) old object # centroids with (2) the newly computed object centroids objects = ct.update(rects) # 在下一个步骤中,我们将回顾逻辑,该逻辑计算一个人是否在框架中向上或向下移动: # loop over the tracked objects for (objectID, centroid) in objects.items(): # check to see if a trackable object exists for the current # object ID to = trackableObjects.get(objectID, None) if to is None: to = TrackableObject(objectID, centroid) # otherwise, there is a trackable object so we can utilize it # to determine direction else: # the difference between the y-coordinate of the *current* # centroid and the mean of *previous* centroids will tell # us in which direction the object is moving (negative for # 'up' and positive for 'down') """ 我们获取给定对象的所有先前质心位置的y坐标值。 然后,我们通过获取current-object当前质心位置与current-object所有先前质心位置的平均值之间的差来计算方向。 我们之所以这样做是为了确保我们的方向跟踪更加稳定。 如果我们仅存储该人的先前质心位置,则我们可能会错误地计算方向。 """ to.centroids.append(centroid) # check to see if the object has been counted or not if not to.counted: # if the direction is negative (indicating the object # is moving up) AND the centroid is above the center # line, count the object """ 检查方向是否为负(指示对象正在向上移动)以及质心是否在中心线上方。 在这种情况下,我们增加 totalUp 。 """ if to.centroids[0][1] < int( H * cfg.FRAME.LINE) and centroid[1] > int( H * cfg.FRAME.LINE): total_down += 1 to.counted = True to.flag = 'DOWN' elif to.centroids[0][1] > int( H * cfg.FRAME.LINE) and centroid[1] < int( H * cfg.FRAME.LINE): total_up += 1 to.counted = True to.flag = 'UP' # store the trackable object in our dictionary trackableObjects[objectID] = to # 屏显 # draw both the ID of the object and the centroid of the # object on the output frame text = "ID {}".format(objectID) if to.counted: cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.circle(frame, (centroid[0], centroid[1]), 4, (0, 0, 255), -1) else: cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2) cv2.circle(frame, (centroid[0], centroid[1]), 4, (0, 0, 255), -1) # construct a tuple of information we will be displaying on the # frame info = [ ("Up", total_up), ("Down", total_down), ("Status", status), ] print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>totalDown", total_down) print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>totalUp", total_up) # loop over the info tuples and draw them on our frame for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) if cfg.DATA.OUTPUT is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(cfg.DATA.OUTPUT, fourcc, 30, (W, H), True) # 写入操作 # check to see if we should write the frame to disk if writer is not None: writer.write(frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # increment the total number of frames processed thus far and # then update the FPS counter end_time = time.time() inference_times.append(end_time - st) totalFrames += 1 fps.update() # stop the timer and display FPS information try: inference_time = sum(inference_times) / len(inference_times) # 计算FPS fps1 = 1.0 / inference_time # FPS计算方式 print("---------------------------------------------------------") print("FPS is ..............{}".format(fps1)) except Exception as e: print(e) fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) print('totaldown people:', total_down) print('totalup people:', total_up) # 写测试信息 with open(info_txt, 'w') as f: f.write("[INFO] elapsed time: " + str("{:.2f}".format(fps.elapsed())) + "\n") f.write("[INFO] approx. FPS: " + str("{:.2f}".format(fps.fps())) + "\n") f.write('totaldown people: ' + str(total_down) + "\n") f.write('totalup people: ' + str(total_up)) # release the video capture vs.release() # check to see if we need to release the video writer pointer if writer is not None: writer.release() # close any open windows cv2.destroyAllWindows()
def YOLO(): global metaMain, netMain, altNames configPath = "./yolov3.cfg" weightPath = "./yolov3.weights" metaPath = "./coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath)+"`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode( "ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass cap = cv2.VideoCapture('http://192.168.0.11:4747/video') # cap = cv2.VideoCapture("project_video.mp4") # out = cv2.VideoWriter( # "output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0, # (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain),3) fps = FPS().start() trackers = [] labels = [] while True: prev_time = time.time() ret, frame_read = cap.read() frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.8) if len(trackers) == 0: for detection in detections: if detection[0].decode() == 'stop sign': x, y, w, h = detection[2][0], \ detection[2][1], \ detection[2][2], \ detection[2][3] xmin, ymin, xmax, ymax = convertBack( float(x), float(y), float(w), float(h)) pt1 = (xmin, ymin) pt2 = (xmax, ymax) label = detection[0].decode() t = dlib.correlation_tracker() rect = dlib.rectangle(xmin, ymin, xmax, ymax) t.start_track(frame_resized, rect) labels.append(label) trackers.append(t) cv2.rectangle(frame_resized, pt1, pt2, (0, 255, 0), 1) cv2.putText(frame_resized, detection[0].decode() + " [" + str(round(detection[1] * 100)) + "]", (pt1[0], pt1[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 255, 0], 2) else: # loop over each of the trackers for (t, l) in zip(trackers, labels): # update the tracker and grab the position of the tracked # object t.update(frame_resized) pos = t.get_position() # unpack the position object startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) # draw the bounding box from the correlation object tracker cv2.rectangle(frame_resized, (startX, startY), (endX, endY), (0, 255, 0), 1) cv2.putText(frame_resized, l, (startX, startY - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2RGB) cv2.imshow('Demo', image) keys = cv2.waitKey(1) & 0xFF fps.update() print(labels) if keys == ord("q"): break cap.release() fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
def main(): print("Yo") parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-m', '--model', required=True, help='File path of .tflite file.') parser.add_argument('-l', '--labels', help='File path of labels file.') parser.add_argument('-t', '--threshold', type=float, default=0.4, help='Score threshold for detected objects.') parser.add_argument('-o', '--output', help='File path for the result image with annotations') parser.add_argument('-c', '--count', type=int, default=5, help='Number of times to run inference') args = parser.parse_args() labels = load_labels(args.labels) if args.labels else {} interpreter = make_interpreter(args.model) interpreter.allocate_tensors() # initialize the camera and grab a reference to the raw camera capture # camera = PiCamera() resolution = (1280, 720) # camera.resolution = resolution # camera.framerate = 30 freq = cv2.getTickFrequency() # rawCapture = PiRGBArray(camera, size=resolution) fps = FPS().start() piVideoStream = VideoStream(usePiCamera=True, resolution=resolution, framerate=30).start() time.sleep(1) while True: t0 = cv2.getTickCount() frame = piVideoStream.read() fps.update() image_rgb_np = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # image_rgb_np_with_detections = doinference(image_rgb_np) # image_bgr_np_with_detections = cv2.cvtColor(image_rgb_np_with_detections, cv2.COLOR_RGB2BGR) # cv2.putText(frame, 'FPS: {0:.2f}'.format(fps.fps()), (30, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, # (255, 255, 0), 2, cv2.LINE_AA) # scale = detect.set_input(interpreter, resolution, lambda size: cv2.resize(image_rgb_np, size)) interpreter.invoke() objs = detect.get_output(interpreter, args.threshold, scale) draw_objects(frame, objs, labels) # for obj in objs: # print(labels.get(obj.id, obj.id)) # print(' id: ', obj.id) # print(' score: ', obj.score) # print(' bbox: ', obj.bbox) cv2.imshow('frame', frame) # # t1 = cv2.getTickCount() # time= (t1-t0)/freq # fps = 1/time # # clear the stream in preparation for the next frame # rawCapture.truncate(0) # # resets the time if cv2.waitKey(1) & 0xFF == ord('q'): break fps.stop() piVideoStream.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows()
def app_function(): # initialize the list of class labels MobileNet SSD was trained to detect # generate a set of bounding box colors for each class CLASSES = [ 'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ] #CLASSES = ['motorbike', 'person'] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe('MobileNetSSD_deploy.prototxt.txt', 'MobileNetSSD_deploy.caffemodel') print('Loading helmet model...') loaded_model = load_model('new_helmet_model.h5') loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) # initialize the video stream, print("[INFO] starting video stream...") # Loading the video file cap = cv2.VideoCapture('v.mp4') # time.sleep(2.0) # Starting the FPS calculation fps = FPS().start() # loop over the frames from the video stream # i = True while True: # i = not i # if i==True: try: # grab the frame from the threaded video stream and resize it # to have a maxm width and height of 600 pixels ret, frame = cap.read() # resizing the images frame = imutils.resize(frame, width=600, height=600) # grab the frame dimensions and convert it to a blob (h, w) = frame.shape[:2] # Resizing to a fixed 300x300 pixels and normalizing it. # Creating the blob from image to give input to the Caffe Model blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) # pass the blob through the network and obtain the detections and predictions net.setInput(blob) detections = net.forward( ) # getting the detections from the network persons = [] person_roi = [] motorbi = [] # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence associated with the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the confidence # is greater than minimum confidence if confidence > 0.5: # extract index of class label from the detections idx = int(detections[0, 0, i, 1]) if idx == 15: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # roi = box[startX:endX, startY:endY/4] # person_roi.append(roi) persons.append((startX, startY, endX, endY)) if idx == 14: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") motorbi.append((startX, startY, endX, endY)) xsdiff = 0 xediff = 0 ysdiff = 0 yediff = 0 p = () for i in motorbi: mi = float("Inf") for j in range(len(persons)): xsdiff = abs(i[0] - persons[j][0]) xediff = abs(i[2] - persons[j][2]) ysdiff = abs(i[1] - persons[j][1]) yediff = abs(i[3] - persons[j][3]) if (xsdiff + xediff + ysdiff + yediff) < mi: mi = xsdiff + xediff + ysdiff + yediff p = persons[j] # r = person_roi[j] if len(p) != 0: # display the prediction label = "{}".format(CLASSES[14]) print("[INFO] {}".format(label)) cv2.rectangle(frame, (i[0], i[1]), (i[2], i[3]), COLORS[14], 2) y = i[1] - 15 if i[1] - 15 > 15 else i[1] + 15 cv2.putText(frame, label, (i[0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[14], 2) label = "{}".format(CLASSES[15]) print("[INFO] {}".format(label)) cv2.rectangle(frame, (p[0], p[1]), (p[2], p[3]), COLORS[15], 2) y = p[1] - 15 if p[1] - 15 > 15 else p[1] + 15 roi = frame[p[1]:p[1] + (p[3] - p[1]) // 4, p[0]:p[2]] print(roi) if len(roi) != 0: img_array = cv2.resize(roi, (50, 50)) gray_img = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY) img = np.array(gray_img).reshape(1, 50, 50, 1) img = img / 255.0 prediction = loaded_model.predict_proba([img]) cv2.rectangle(frame, (p[0], p[1]), (p[0] + (p[2] - p[0]), p[1] + (p[3] - p[1]) // 4), COLORS[0], 2) cv2.putText(frame, str(round(prediction[0][0], 2)), (p[0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[0], 2) except: pass cv2.imshow('Frame', frame) # Displaying the frame key = cv2.waitKey(1) & 0xFF if key == ord('q'): # if 'q' key is pressed, break from the loop break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() cap.release() # Closing the video stream return homepage()
class DepthAI: def __init__( self, file=None, camera=False, ): print("Loading pipeline...") self.file = file self.camera = camera self.fps_cam = FPS() self.fps_nn = FPS() self.create_pipeline() self.start_pipeline() self.fontScale = 1 if self.camera else 2 self.lineType = 0 if self.camera else 3 def create_pipeline(self): print("Creating pipeline...") self.pipeline = depthai.Pipeline() if self.camera: # ColorCamera print("Creating Color Camera...") self.cam = self.pipeline.createColorCamera() self.cam.setPreviewSize(self._cam_size[1], self._cam_size[0]) self.cam.setResolution( depthai.ColorCameraProperties.SensorResolution.THE_4_K) self.cam.setInterleaved(False) self.cam.setBoardSocket(depthai.CameraBoardSocket.RGB) self.cam.setColorOrder( depthai.ColorCameraProperties.ColorOrder.BGR) self.cam_xout = self.pipeline.createXLinkOut() self.cam_xout.setStreamName("preview") self.cam.preview.link(self.cam_xout.input) self.create_nns() print("Pipeline created.") def create_nns(self): pass def create_nn(self, model_path: str, model_name: str, first: bool = False): """ :param model_path: model path :param model_name: model abbreviation :param first: Is it the first model :return: """ # NeuralNetwork print(f"Creating {model_path} Neural Network...") model_nn = self.pipeline.createNeuralNetwork() model_nn.setBlobPath(str(Path(f"{model_path}").resolve().absolute())) model_nn.input.setBlocking(False) if first and self.camera: print("linked cam.preview to model_nn.input") self.cam.preview.link(model_nn.input) else: model_in = self.pipeline.createXLinkIn() model_in.setStreamName(f"{model_name}_in") model_in.out.link(model_nn.input) model_nn_xout = self.pipeline.createXLinkOut() model_nn_xout.setStreamName(f"{model_name}_nn") model_nn.out.link(model_nn_xout.input) def start_pipeline(self): self.device = depthai.Device(self.pipeline) print("Starting pipeline...") self.device.startPipeline() self.start_nns() if self.camera: self.preview = self.device.getOutputQueue(name="preview", maxSize=4, blocking=False) def start_nns(self): pass def put_text(self, text, dot, color=(0, 0, 255), font_scale=None, line_type=None): font_scale = font_scale if font_scale else self.fontScale line_type = line_type if line_type else self.lineType dot = tuple(dot[:2]) cv2.putText( img=self.debug_frame, text=text, org=dot, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=font_scale, color=color, lineType=line_type, ) def parse(self): if debug: self.debug_frame = self.frame.copy() self.parse_fun() if debug: cv2.imshow( "Camera_view", self.debug_frame, ) self.fps_cam.update() if cv2.waitKey(1) == ord("q"): cv2.destroyAllWindows() self.fps_cam.stop() self.fps_nn.stop() print( f"FPS_CAMERA: {self.fps_cam.fps():.2f} , FPS_NN: {self.fps_nn.fps():.2f}" ) raise StopIteration() def parse_fun(self): pass def run_video(self): cap = cv2.VideoCapture(str(Path(self.file).resolve().absolute())) while cap.isOpened(): read_correctly, self.frame = cap.read() if not read_correctly: break try: self.parse() except StopIteration: break cap.release() def run_camera(self): while True: in_rgb = self.preview.tryGet() if in_rgb is not None: shape = (3, in_rgb.getHeight(), in_rgb.getWidth()) self.frame = (in_rgb.getData().reshape(shape).transpose( 1, 2, 0).astype(np.uint8)) self.frame = np.ascontiguousarray(self.frame) try: self.parse() except StopIteration: break @property def cam_size(self): return self._cam_size @cam_size.setter def cam_size(self, v): self._cam_size = v def run(self): self.fps_cam.start() self.fps_nn.start() if self.file is not None: self.run_video() else: self.run_camera() del self.device
def initRecognizer(self, *, lookupLabel, markAsPresent, imageDisplayHandler=None, cameraDevice=0): assert callable(lookupLabel) assert callable(markAsPresent) assert callable(imageDisplayHandler) # initialize the video stream, then allow the camera sensor to warm up print("[INFO] starting video stream...") vs = VideoStream(src=cameraDevice).start() # start the FPS throughput estimator fps = FPS().start() def readFrameAndDisplay(setFrameImage): # grab the frame from the threaded video stream frame = vs.read() if frame is None: return # resize the frame to have a width of 600 pixels (while # maintaining the aspect ratio), and then grab the image # dimensions frame = imutils.resize(frame, width=600) (h, w) = frame.shape[:2] # construct a blob from the image imageBlob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) # apply OpenCV's deep learning-based face detector to localize # faces in the input image self.detector.setInput(imageBlob) detections = self.detector.forward() # loop over the detections for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the prediction confidence = detections[0, 0, i, 2] # filter out weak detections if confidence > self.confidence: # compute the (x, y)-coordinates of the bounding box for # the face box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") cv2.rectangle(frame, (startX, startY), (endX, endY), (194, 188, 200), 2) # extract the face ROI face = frame[startY:endY, startX:endX] (fH, fW) = face.shape[:2] # ensure the face width and height are sufficiently large if fW < 20 or fH < 20: continue # construct a blob for the face ROI, then pass the blob # through our face embedding model to obtain the 128-d # quantification of the face faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False) self.embedder.setInput(faceBlob) vec = self.embedder.forward() # perform classification to recognize the face preds = self.svcRecognizer.predict_proba(vec)[0] j = np.argmax(preds) proba = preds[j] matricCode = self.labelEncoder.classes_[j] name = lookupLabel(matricCode) if proba < self.confidence: continue if name: # draw the bounding box of the face along with the # associated probability text = "{}: {:.2f}%".format(name, proba * 100) y = startY - 10 if startY - 10 > 10 else startY + 10 cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_COMPLEX, 0.55, (0, 0, 256), 2) print("DETECTED [%s] (confidence=%.2f%%)" % (name, proba * 100)) markAsPresent(matricCode) # update the FPS counter fps.update() # show the output frame setFrameImage(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) # loop over frames from the video file stream imageDisplayHandler(readFrameAndDisplay) # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) vs.stop()
(10, H - ((i * 20) + 20)), font, 0.6, (0, 255, 255), 1, cv2.LINE_AA, ) # increment the total number of frames processed thus far and # then update the FPS counter elapsedFrames += 1 fps.update() # time remaining estimator for local files processing if elapsedFrames % FPSUpdate == 0: fps.stop() liveFPS = fps.fps() fps = FPS().start() cv2.putText( frame, "FPS: {:.1f}".format(liveFPS), (0, 15), font, 0.5, (0, 255, 255), 1, cv2.LINE_AA, ) """# draw metadatas on the frame cv2.putText(
def plant_growth_health_tracking(): # define the paths to the Keras deep learning model and label binarizer file MODEL_PATH = "plantgrowth.model" LABELBIN_PATH = "label_bin_plantgrowth.pickle" # initialize the total number of frames that *consecutively* contain # santa along with threshold required to trigger the growthchange state TOTAL_CONSEC = 0 TOTAL_THRESH = 20 # initialize whether the growthchange state has been triggered GrowthChange = False # load the model print("loading model...") model = load_model(MODEL_PATH) lb = pickle.loads(open(LABELBIN_PATH, "rb").read()) # initialize the video stream and allow the camera sensor to warm up print("starting video stream...") #vs = VideoStream(src=0).start() vs = VideoStream(usePiCamera=True).start() time.sleep(2.0) fps = FPS().start() # loop over the frames from the video stream while fps._numFrames<100: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=400) #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #a typical green will have still have some red and blue #we will get the lower-light mixes of all colors still lower_green = np.array([30,100,100]) upper_green = np.array([90,255,255]) #create a mask for a specific range #pixels in the range will be converted to pure white #pixels outside the range will be converted to black mask = cv2.inRange(hsv, lower_green, upper_green) #restore 'green-ness' by running a bitwise operation #show color when there is the frame AND the mask res = cv2.bitwise_and(frame,frame, mask= mask) date_yyyymmdd = time.strftime("%Y-%m-%d") #saves 5 color filtered images in JPG format in the working directory for post for index in range(1,6): cv2.imwrite(str(date_yyyymmdd)+"_pic_"+str(index)+".jpg", res) # prepare the image to be classified by our deep learning network image = cv2.resize(frame, (96, 96)) image = image.astype("float") / 255.0 image = img_to_array(image) image = np.expand_dims(image, axis=0) # classify the input image and initialize the label and # probability of the prediction #calculate probabilities proba = model.predict(image)[0] idx = np.argmax(proba) #create the label label = lb.classes_[idx] # check to see if santa was detected using our convolutional neural network if proba[idx] > 0.8: # increment the total number of consecutive frames that contain the plant type TOTAL_CONSEC += 1 # check to see if we should raise the growthchange state if not GrowthChange and TOTAL_CONSEC >= TOTAL_THRESH: # indicate that santa has been found GrowthChange = True # send a text notification to firebase for app to receive triggered("Young seedling has matured. Time for transplant.") # otherwise, reset the total number of consecutive frames and the growthchange state else: TOTAL_CONSEC = 0 GrowthChange = False # build the label and draw it on the frame label = "{}: {:.2f}%".format(label, proba[idx] * 100) frame = cv2.putText(frame, label, (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2) # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break fps.update() # do a bit of cleanup print("cleaning up...") fps.stop() cv2.destroyAllWindows() vs.stop()
# check to see if the frame should be displayed to our screen if args["display"] > 0: cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # clear the stream in preparation for the next frame and update # the FPS counter rawCapture.truncate(0) fps.update() # check to see if the desired number of frames have been reached if i == args["num_frames"]: break # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() stream.close() rawCapture.close() camera.close() # created a *threaded *video stream, allow the camera sensor to warmup, # and start the FPS counter print("[INFO] sampling THREADED frames from `picamera` module...") vs = PiVideoStream().start() time.sleep(2.0) fps = FPS().start()
def start_app(): # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--prototxt", required=True, help="path to Caffe 'deploy' prototxt file") ap.add_argument("-m", "--model", required=True, help="path to Caffe pre-trained model") ap.add_argument("-c", "--confidence", type=float, default=0.3, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) # initialize the list of class labels MobileNet SSD was trained to # detect, then generate a set of bounding box colors for each class CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # initialize the input queue (frames), output queue (detections), # and the list of actual detections returned by the child process inputQueue = Queue(maxsize=1) outputQueue = Queue(maxsize=1) detections = None # construct a child process *indepedent* from our main process of # execution print("[INFO] starting process...") p = Process(target=classify_frame, args=(net, inputQueue, outputQueue,)) p.daemon = True p.start() # initialize the video stream, allow the cammera sensor to warmup, # and initialize the FPS counter print("[INFO] starting video stream...") #vs = VideoStream(src=0).start() #vs = VideoStream(usePiCamera=True).start() vs = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER) time.sleep(2.0) fps = FPS().start() cnt = 0 found_tmp = "chair" # loop over the frames from the video stream while True: # grab the frame from the threaded video stream, resize it, and # grab its imensions ret, frame = vs.read() #frame = cv2.flip(frame,0) frame = cv2.resize(frame, (400,400)) frame = imutils.resize(frame, width=400) #400 (fH, fW) = frame.shape[:2] # if the input queue *is* empty, give the current frame to # classify if inputQueue.empty(): inputQueue.put(frame) # if the output queue *is not* empty, grab the detections if not outputQueue.empty(): detections = outputQueue.get() # check to see if our detectios are not None (and if so, we'll # draw the detections on the frame) if detections is not None: # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence (i.e., probability) associated # with the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` # is greater than the minimum confidence if confidence < args["confidence"]: continue # otherwise, extract the index of the class label from # the `detections`, then compute the (x, y)-coordinates # of the bounding box for the object idx = int(detections[0, 0, i, 1]) dims = np.array([fW, fH, fW, fH]) box = detections[0, 0, i, 3:7] * dims (startX, startY, endX, endY) = box.astype("int") # draw the prediction on the frame label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100) pygame.mixer.init() pygame.mixer.music.load("exa.mp3") pygame.mixer.music.set_volume(1) longitude = 1.123213 latitude = 29.545345 label_tmp = "\""+label+"\"" #print(label) if "dog" in label or "cat" in label or "horse" in label or "cow" in label: cv2.rectangle(frame, (startX, startY), (endX, endY),COLORS[idx], 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) data = {"value1":latitude,"value2":longitude,"value3":label_tmp} pygame.mixer.music.play() animal = label.split(":") pet = str(animal[0]) print(pet) if cnt == 0 and found_tmp != pet: resp = requests.post('http://cloud.park-cloud.co19.kr/jetson_nano/post-data.php', params=data) cnt = cnt + 1 found_tmp = pet else: cnt=0 # show the output frame cv2.namedWindow("Frame", cv2.WINDOW_NORMAL) cv2.setWindowProperty("Frame", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) #up-side reverse #frame = cv2.flip(frame,0) #fra me = np.array(frame ,dtype = object) frame = cv2.resize(frame, (720,480)) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()