def __init__(self, proxy_map): super(SpecificWorker, self).__init__(proxy_map) self.timer.timeout.connect(self.compute) self.Period = 20 self.timer.start(self.Period) self.model = '' self.prototxt = '' self.embedding_model = '' self.confidence = 0.5 self.ct = CentroidTracker() self.net = None self.embedder = None self.recognizer = None self.label_encoder = None self.writting = False self.name_dir = '' self.faces = None # lo que se envia a humanPose # interfaz self.ui.name_lineEdit.textEdited.connect(self.name_changed) self.ui.name_lineEdit.editingFinished.connect(self.name_finished) self.ui.add_person_button.clicked.connect(self.add_person) self.ui.train_button.clicked.connect(self.update_model)
args = vars(ap.parse_args()) with open(os.path.join(args["yolo"], "coco.names"), "rt") as f: classes = f.read().rstrip("\n").split("\n") cfg = os.path.join(args["yolo"], "yolov3-tiny.cfg") weights = os.path.join(args['yolo'], "yolov3-tiny.weights") logging.info("Loading Model ...") net = cv2.dnn.readNetFromDarknet(cfg, weights) writer = None W = None H = None ct = CentroidTracker(maxDisappeared=40) trackers = [] trackableObject = {} prev_sum = 0 prev_roi = np.array([0, 0, 0]) prev_dist = 0 active_obj = {} totalFrames = 0 def check_active(): try: for id, obj in active_obj.items(): if "frames_not_active" in obj and obj["frames_not_active"] > 120:
class SpecificWorker(GenericWorker): def __init__(self, proxy_map): super(SpecificWorker, self).__init__(proxy_map) self.timer.timeout.connect(self.compute) self.Period = 20 self.timer.start(self.Period) self.model = '' self.prototxt = '' self.embedding_model = '' self.confidence = 0.5 self.ct = CentroidTracker() self.net = None self.embedder = None self.recognizer = None self.label_encoder = None self.writting = False self.name_dir = '' self.faces = None # lo que se envia a humanPose # interfaz self.ui.name_lineEdit.textEdited.connect(self.name_changed) self.ui.name_lineEdit.editingFinished.connect(self.name_finished) self.ui.add_person_button.clicked.connect(self.add_person) self.ui.train_button.clicked.connect(self.update_model) def read_files(self): print("[INFO] loading face detector...") self.model = "/home/robocomp/robocomp/components/robocomp-viriato/components/faceTracking/files/res10_300x300_ssd_iter_140000.caffemodel" self.prototxt = "/home/robocomp/robocomp/components/robocomp-viriato/components/faceTracking/files/deploy.prototxt" self.net = cv2.dnn.readNetFromCaffe(self.prototxt, self.model) print("[INFO] loading face recognizer...") self.embedding_model = "/home/robocomp/robocomp/components/robocomp-viriato/components/faceTracking/files/openface_nn4.small2.v1.t7" self.embedder = cv2.dnn.readNetFromTorch(self.embedding_model) recog= "/home/robocomp/robocomp/components/robocomp-viriato/components/faceTracking/files/output/recognizer.pickle" le = "/home/robocomp/robocomp/components/robocomp-viriato/components/faceTracking/files/output/le.pickle" self.recognizer = pickle.loads(open(recog).read()) self.label_encoder = pickle.loads(open(le).read()) print (CURRENT_PATH) def setParams(self, params): self.read_files() self.show() return True def name_changed(self,text): self.name_dir = text self.writting = True def name_finished(self): self.writting = False def time_Lapse(self, dir): QMessageBox().information(self.focusWidget(), 'Time Lapse', 'For a better recognition rotate the face in several directions. Push OK to start', QMessageBox.Ok) for count in range(10): color, _, _, _ = self.rgbd_proxy.getData() frame = np.fromstring(color, dtype=np.uint8) frame = np.reshape(frame, (480, 640, 3)) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # (h, w) = frame.shape[:2] # blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, # (300, 300), (104.0, 177.0, 123.0)) # # self.net.setInput(blob) # detections = self.net.forward() # # for i in range(0, detections.shape[2]): # # confidence = detections[0, 0, i, 2] # if confidence >self.confidence: # # box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) # (startX, startY, endX, endY) = box.astype("int") # startX = startX -100 # startY = startY -100 # endX = endX + 100 # endY = endY + 100 # # face = frame[startY:endY, startX:endX] # pass the blob through the network and obtain the detections and # predictions print ("Saving frame%d.jpg" % count) name_file = os.path.join(dir, "frame%d.jpg" % count) cv2.imwrite(name_file, frame) time.sleep(0.2) def add_person(self): self.writting = True if (self.name_dir.strip() == "") : print ("No name inserted") return dir = os.path.join(CURRENT_PATH,"../files/dataset", self.name_dir.strip().lower()) #se crea el directorio en minusculas y sin espacios print (dir) if os.path.isdir(dir): reply = QMessageBox.question(self.focusWidget(), 'The directory already exists', ' Do you want to overwrite it?', QMessageBox.Yes, QMessageBox.No) if reply == QtGui.QMessageBox.No: self.ui.name_lineEdit.clear() self.writting = False return else: self.time_Lapse(dir) else: os.mkdir(dir) self.time_Lapse(dir) self.update_model() self.ui.name_lineEdit.clear() self.writting = False def update_model(self): reply2 = QMessageBox.question(self.focusWidget(), 'The model needs to be trained', ' Do you want to train the model? It may take a while', QMessageBox.Yes, QMessageBox.No) if reply2 == QtGui.QMessageBox.No: return else: ExEm.extract_embeddings() TM.train_model() self.read_files() print ("[INFO] Model updated ") @QtCore.Slot() def compute(self): if self.writting is False: color,_, _, _ = self.rgbd_proxy.getData() frame = np.fromstring(color, dtype=np.uint8) frame = np.reshape(frame,(480, 640, 3)) (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0)) # pass the blob through the network and obtain the detections and # predictions self.net.setInput(blob) detections = self.net.forward() rects = [] bounding_boxes = [] for i in range(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence >self.confidence: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") qr = QRect(QPoint(startX, endY), QPoint(endX,startY)) bounding_boxes.append(qr) rects.append(box.astype("int")) objects = self.ct.update(rects) # loop over the tracked objects faces_found = [] for (objectID, centroid) in objects.items(): faceT = TFace() tracking = False for qrects in bounding_boxes: if qrects.contains(QPoint(centroid[0],centroid[1])): faceT.tracking = True bbox = Box() (bbox.posx, bbox.posy, bbox.width,bbox.height) = qrects.getRect() faceT.boundingbox = bbox tracking = True (startX, endY , endX, startY) = qrects.getCoords() # 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 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.recognizer.predict_proba(vec)[0] j = np.argmax(preds) proba = preds[j] *100 if (proba < 50): name = "unknow" else: name = self.label_encoder.classes_[j] # draw the bounding box of the face along with the # associated probability text = "{}: {:.2f}%".format(name, proba) y = startY - 10 if startY - 10 > 10 else startY + 10 cv2.rectangle(frame, (startX, startY), (endX, endY), (255, 10, 150), 2) cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (255, 10, 150), 2) faceT.name = name faceT.confidence = proba*100 break if (tracking == False): faceT.tracking = False pnt = Point() pnt.x = centroid[0] pnt.y = centroid [1] faceT.id = objectID faceT.centroid = pnt # 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, (255, 10, 150), 2) cv2.circle(frame, (centroid[0], centroid[1]), 4, (255, 10, 150), -1) faces_found.append(faceT) self.faces = faces_found height, width, channel = frame.shape bytesPerLine = 3 * width qImg = QImage(frame.data, width, height, bytesPerLine, QImage.Format_RGB888) self.ui.image_label.setPixmap(QPixmap.fromImage(qImg)) return True # # getFaces # def getFaces(self): return self.faces
cap.set(cv.CAP_PROP_FOURCC, fourcc) # #Decrease frame size # # cap.set(5, 10) # cap.set(cv.CAP_PROP_FPS, 120) # # cap.set(cv.CAP_PROP_EXPOSURE, -7 ) # cap.set(cv.CAP_PROP_FRAME_WIDTH, 640) # cap.set(cv.CAP_PROP_FRAME_HEIGHT, 480) # vs = WebcamVideoStream(src=1).start() fgbg = cv.createBackgroundSubtractorMOG2() kernel_dil = np.ones((20, 20), np.uint8) kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (3, 3)) ct = CentroidTracker() trackableObjects = {} # 418 37 # 1357 86 # 429 814 # 1405 750 totalHit = 0 WaitingFrame = 0 i = 0 # (H, W) = (1600, 869) (H, W) = (1920, 1080) while (1): rec, frame = cap.read() fps = cap.get(cv.CAP_PROP_FPS) print("fps = " + str(fps))
def detect_video(yolo, video_path, output_path=""): if video_path.isdigit(): video_path = int(video_path) vid = cv2.VideoCapture(video_path) if not vid.isOpened(): raise IOError("Couldn't open webcam or video") video_FourCC = cv2.VideoWriter_fourcc(*"mp4v") video_fps = vid.get(cv2.CAP_PROP_FPS) video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))) # print("input video size: {}".format(video_size)) isOutput = True if output_path != "" else False if isOutput: # print("!!! TYPE:", type(output_path), type(video_FourCC), type(video_fps), type(video_size)) out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size) # fgbg = cv2.createBackgroundSubtractorKNN() fgbg = cv2.bgsegm.createBackgroundSubtractorGSOC() ct = CentroidTracker(maxDisappeared=40, maxDistance=90) trackableObjects = {} del_ID = 0 count_a = 0 count_b = 0 flag = False max_area = 0 min_area = video_size[0] * video_size[1] area_time = 0 accum_time = 0 curr_fps = 0 max_fps = 0 fps = "FPS: ??" n = 0 sum_fps = 0 prev_time = timer() while True: _, frame = vid.read() if type(frame) == type(None): break resize_img = cv2.resize(frame, (frame.shape[1] // 3, frame.shape[0] // 3)) mask = fgbg.apply(resize_img) # mask1 = mask # thresh = cv2.threshold(mask, 3, 255, cv2.THRESH_BINARY)[1] cv2.namedWindow('maskwindow', cv2.WINDOW_NORMAL) cv2.imshow('maskwindow', mask) if flag: out_image = frame contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for _, cnt in enumerate(contours): area = cv2.contourArea(cnt) if area > min_area and area < (max_area): flag = False break else: image = Image.fromarray(frame) image, out_boxes, out_scores = yolo.detect_image(image) out_boxes = get_area(mask, out_boxes, out_scores) # if len(out_boxes) != 0: # for i, box in enumerate(out_boxes): # cv2.rectangle(frame, (box[1] // 3, box[0] // 3), (box[3] // 3, box[2] // 3), (127, 255, 0), thickness=1) objects = ct.update(out_boxes) color_list = get_color(frame, objects) image, count_b, count_a = track_objects(image, objects, count_b, count_a, trackableObjects, color_list) out_image = np.asarray(image) if len(out_boxes) != 0: for i, box in enumerate(out_boxes): cv2.rectangle(out_image, (box[1], box[0]), (box[3], box[2]), (127, 255, 0), thickness=2) if len(objects) != 0: objects_ID = list(objects.keys()) trackable_ID = list(trackableObjects.keys()) non_IDs = list(set(trackable_ID) - set(objects_ID)) for non_ID in non_IDs: del trackableObjects[non_ID] if area_time < 150: max_area, min_area = max_min_area(mask, out_boxes, out_scores, max_area, min_area) area_time += 1 elif len(objects) == 0 and area_time >= 150: flag = True cv2.line(out_image, (0, count_line(out_image.shape[1], out_image.shape[0], 0)), (out_image.shape[1], count_line(out_image.shape[1], out_image.shape[0], out_image.shape[1])), color=(127, 255, 0), thickness=3) curr_time = timer() exec_time = curr_time - prev_time prev_time = curr_time accum_time = accum_time + exec_time curr_fps = curr_fps + 1 if accum_time > 1: accum_time = accum_time - 1 fps = "FPS: " + str(curr_fps) if max_fps < curr_fps: max_fps = curr_fps sum_fps += curr_fps n += 1 curr_fps = 0 print(fps) cv2.putText(out_image, text=fps, org=(3, 15), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.50, color=(127, 255, 0), thickness=2) info = [("count B", count_b), ("coutn A", count_a), ("min area", min_area), ("max area", max_area)] for (i, (k, v)) in enumerate(info): textInfo = "{}: {}".format(k, v) cv2.putText(out_image, text=textInfo, org=(10, out_image.shape[0] - ((30 * i) + 20)), fontFace=cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.7, color=(127, 255, 0), thickness=1) cv2.namedWindow("result", cv2.WINDOW_NORMAL) cv2.imshow("result", out_image) # cv2.namedWindow('maskwindow', cv2.WINDOW_NORMAL) # cv2.imshow('maskwindow', mask1) if isOutput: out.write(out_image) if cv2.waitKey(1) & 0xFF == ord('q'): break print("Max area: {}, Min area: {}".format(max_area, min_area)) print("Max FPS: {}FPS".format(max_fps)) print("Ave FPS: {:.2f}FPS".format(sum_fps / n)) yolo.close_session()
"--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.5, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) # initialize our centroid tracker and frame dimensions ct = CentroidTracker() (H, W) = (None, None) # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # initialize the video stream and allow the camera sensor to warmup print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) # loop over the frames from the video stream while True: # read the next frame from the video stream and resize it frame = vs.read()
# initialize the video stream and allow the camera sensor to warmup print("[INFO] warming up camera...") vs = VideoStream(src=0).start() #vs = cv2.VideoCapture(args["input"]) time.sleep(2.0) # initialize the frame dimensions (we'll set them as soon as we read # the first frame from the video) H = None W = 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=conf["max_disappear"], maxDistance=conf["max_distance"]) trackers = [] trackableObjects = {} # keep the count of total number of frames totalFrames = 0 # initialize the log file logFile = None # initialize the list of various points used to calculate the avg of # the vehicle speed points = [("A", "B"), ("B", "C"), ("C", "D")] # start the frames per second throughput estimator fps = FPS().start()
if not args.get("input", False): print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) else: print("[INFO] opening video file...") vs = cv2.VideoCapture(args["input"]) writer = None W = None H = None ct = CentroidTracker(maxDisappeared=10, maxDistance=30) trackers = [] trackableObjects = {} totalFrames = 0 totalDown = 0 totalUp = 0 fps = FPS().start() while True: frame = vs.read() frame = frame[1] if args.get("input", False) else frame if args["input"] is not None and frame is None: break
def main(): default_model_dir = '../all_models' default_model = '../all_models/mobilenet_ssd_v2_coco_quant_postprocess_edgetpu.tflite' default_labels = '../all_models/coco_labels.txt' parser = argparse.ArgumentParser() parser.add_argument('--model', help='.tflite model path', default=os.path.join(default_model_dir, default_model)) parser.add_argument('--labels', help='label file path', default=os.path.join(default_model_dir, default_labels)) parser.add_argument( '--top_k', type=int, default=1, help='number of categories with highest score to display') parser.add_argument('--camera_idx', type=str, help='Index of which video source to use. ', default=0) parser.add_argument('--threshold', type=float, default=0.5, help='classifier score threshold') args = parser.parse_args() #================================================================= # initialize the frame dimensions (we'll set them as soon as we read # the first frame from the video) W = 640 H = 480 ct = CentroidTracker() 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 totalDown = 0 totalUp = 0 #=========================================================================== #print('Loading Handtracking model {} with {} labels.'.format(args.model, args.labels)) #engine = DetectionEngine(args.model) #labels = load_labels(args.labels) #===================================================================== src_size = (W, H) print('Loading Detection model {} with {} labels.'.format( args.model, args.labels)) engine2 = DetectionEngine(args.model) labels2 = load_labels(args.labels) cap = cv2.VideoCapture(args.camera_idx) while cap.isOpened(): #while cv2.waitKey(1)<0: ret, frame = cap.read() if not ret: break cv2_im = frame cv2_im_rgb = cv2.cvtColor(cv2_im, cv2.COLOR_BGR2RGB) pil_im = Image.fromarray(cv2_im_rgb) #declare new window for show pose in 2d plane======================== h_cap, w_cap, _ = cv2_im.shape cv2_sodidi = np.zeros((h_cap, w_cap, 3), np.uint8) #================================================================ objs = engine2.detect_with_image(pil_im, threshold=0.2, keep_aspect_ratio=True, relative_coord=True, top_k=3) cv2_im, rects = append_objs_to_img(cv2_im, objs, labels2) #==================tracking================================================= status = "Waiting" cv2.line(cv2_im, (W // 2, 0), (W // 2, H), (0, 255, 255), 2) objects = ct.update(rects) print(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 if direction < 0 and centroid[1] < H // 2: totalUp += 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 elif direction > 0 and centroid[1] > H // 2: totalDown += 1 to.counted = True # 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(cv2_im, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.circle(cv2_im, (centroid[0], centroid[1]), 4, (0, 255, 0), -1) # construct a tuple of information we will be displaying on the # frame info = [ ("Up", totalUp), ("Down", totalDown), ("Status", status), ] # loop over the info tuples and draw them on our frame for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(cv2_im, text, (10, H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) #====================================================================== cv2.imshow('frame', cv2_im) cv2.imshow('1', cv2_sodidi) #===========print mouse pos===================== cv2.setMouseCallback('frame', onMouse) posNp = np.array(posList) print(posNp) #============streamming to server============== img = np.hstack((cv2_im, cv2_sodidi)) #thay frame = img encoded, buffer = cv2.imencode('.jpg', img) jpg_as_text = base64.b64encode(buffer) footage_socket.send(jpg_as_text) #============================================= if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
# detect CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] # 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 totalDown = 0 totalUp = 0 def count_people(): global vs, outputFrame, lock, W, H, totalUp, totalFrames, totalDown # load our serialized model from disk print("[INFO] loading model...")