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()
def track(self): CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] net = cv2.dnn.readNetFromCaffe(self.p, self.m) vs = VideoStream(src=self.i).start() writer = None W = None H = None ct = CentroidTracker(maxDisappeared=40, maxDistance=50) ct = CentroidTracker(maxDisappeared=40, maxDistance=50) trackers = [] trackableObjects = {} totalFrames = 0 totalDown = 0 totalUp = 0 fps = FPS().start() while True: # grab the next frame and handle if we are reading from either # VideoCapture or VideoStream frame = vs.read() #frame = frame[1] if get(self.i, False) 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 self.i 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 self.o is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(self.o, 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 status = "Waiting" rects = [] # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % self.s == 0: # set the status and initialize our new set of object trackers status = "Detecting" trackers = [] # convert the frame to a blob and pass the blob through the # network and obtain the detections blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5) net.setInput(blob) detections = net.forward() # 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 requiring a minimum # confidence if confidence > self.c: # extract the index of the class label from the # detections list idx = int(detections[0, 0, i, 1]) # if the class label is not a person, ignore it if CLASSES[idx] != "person": continue # compute the (x, y)-coordinates of the bounding box # for the object box = detections[0, 0, i, 3:7] * np.array([W, H, W, H]) (startX, startY, endX, endY) = box.astype("int") # construct a dlib rectangle object from the bounding # box coordinates and then start the dlib correlation # tracker 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) # otherwise, we should utilize our object *trackers* rather than # object *detectors* to obtain a higher frame processing throughput 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)) # draw a horizontal line in the center of the frame -- once an # object crosses this line we will determine whether they were # moving 'up' or 'down' #cv2.line(frame, (0, H // 2), (W, H // 2), (0, 255, 255), 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 (self.objectID, centroid) in objects.items(): # check to see if a trackable object exists for the current # object ID to = trackableObjects.get(self.objectID, None) # if there is no existing trackable object, create one if to is None: to = TrackableObject(self.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[self.objectID] = to # draw both the ID of the object and the centroid of the # object on the output frame text = "ID {}".format(self.objectID) cv2.putText(frame, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.circle(frame, (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(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() #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 not get(self.i, False): # vs.stop() # otherwise, release the video file pointer else: vs.release() # close any open windows cv2.destroyAllWindows()
def VideoPlayerWebcam(drawing_area1, drawing_area2, taskcombobox, model1combobox, model2combobox): cap = cv2.VideoCapture(0) with open('./config.json') as f: data = json.load(f) while (cap.isOpened()): if play1: model = "" try: task = taskcombobox.get_model()[taskcombobox.get_active()][0] model1 = model1combobox.get_model()[ model1combobox.get_active()][0] model2 = model2combobox.get_model()[ model2combobox.get_active()][0] except Exception as e: print(e) for c in data['tasks']: if c["task_name"] == task: for idx, c1 in enumerate(c["model"]): if c1["model_name"] == model1: model1file = c1["model_path"] model1config = c1["model_config_path"] if c1["model_name"] == model2: model2file = c1["model_path"] model2config = c1["model_config_path"] mymutex.acquire() ret, frame = cap.read() if frame is not None: frame1 = frame.copy() frame2 = frame.copy() if task == "face_detection" and model1 == "Haar cascade": face_cascade = cv2.CascadeClassifier(model1file) frame1 = FaceDetection().haarCascade(frame1, face_cascade) elif (task == "face_detection" and (model1 == "MTCNN" or model1 == "Resnet SSD")): filename, ext = os.path.splitext(model1file) if ext == ".caffemodel": net = cv2.dnn.readNetFromCaffe(model1config, model1file) else: net = cv2.dnn.readNetFromTensorflow( model1config, model1file) frame1 = FaceDetection().caffeeAndTensorModel( frame1, net, FPS().start()) elif (task == "face_detection" and (model1 == "HOG dlib" or model1 == "MMOD lib")): if model1 == "HOG dlib": detector = dlib.get_frontal_face_detector() else: detector = dlib.cnn_face_detection_model_v1(model1file) frame1 = FaceDetection().dlib(frame1, detector) elif task == "face_detection" and model1 == "NPD": minFace = 20 maxFace = 4000 overlap = 0.5 f = h5py.File(model1file, 'r') npdModel = { n: np.array(v) for n, v in f.get('npdModel').items() } frame1 = FaceDetection().npd(frame1, npdModel, minFace, maxFace, overlap) webcamframe(drawing_area1, frame1, 1) if task == "face_detection" and model2 == "Haar cascade": face_cascade = cv2.CascadeClassifier(model2file) frame2 = FaceDetection().haarCascade(frame2, face_cascade) elif (task == "face_detection" and (model2 == "MTCNN" or model2 == "Resnet SSD")): filename, ext = os.path.splitext(model2file) if ext == ".caffemodel": net = cv2.dnn.readNetFromCaffe(model2config, model2file) else: net = cv2.dnn.readNetFromTensorflow( model2config, model2file) frame2 = FaceDetection().caffeeAndTensorModel( frame2, net, FPS().start()) elif (task == "face_detection" and (model2 == "HOG dlib" or model2 == "MMOD lib")): if model2 == "HOG dlib": detector = dlib.get_frontal_face_detector() else: detector = dlib.cnn_face_detection_model_v1(model2file) frame2 = FaceDetection().dlib(frame2, detector) elif task == "face_detection" and model2 == "NPD": minFace = 20 maxFace = 4000 overlap = 0.5 f = h5py.File(model2file, 'r') npdModel = { n: np.array(v) for n, v in f.get('npdModel').items() } frame2 = FaceDetection().npd(frame2, npdModel, minFace, maxFace, overlap) webcamframe(drawing_area2, frame2, 2) mymutex.release() time.sleep(0.03) if camrelease: break else: mymutex.release() break cap.release()
def make_prediction(in_filename, out_filename, confidence_threshold=0.3, skip_frames=200, caffe_prototxt_file=None, model_file=None): # initialize the list of class labels MobileNet SSD was trained to # detect CLASSES = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] # load our serialized model from disk print("[INFO] loading model...") # net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) net = model_zoo.get_model('ssd_512_resnet50_v1_voc', pretrained=True) # if a video path was not supplied, grab a reference to the webcam if not in_filename: print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) # otherwise, grab a reference to the video file else: print("[INFO] opening video file...") vs = cv2.VideoCapture(in_filename) # 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=10, 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 totalIn = 0 totalOut = 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 in_filename 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 in_filename 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 out_filename is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(out_filename, 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 status = "Waiting" rects = [] # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % skip_frames == 0: # set the status and initialize our new set of object trackers status = "Detecting" trackers = [] # convert the frame to a blob and pass the blob through the # network and obtain the detections class_IDs, scores, bounding_boxes = net(data.transforms.presets.ssd.transform_test(mx.nd.array(frame), 270)[0]) # loop over the detections for i, (class_ID, score, bounding_box) in enumerate(zip(class_IDs[0], scores[0], bounding_boxes[0])): class_ID = int(class_ID[0].asnumpy()[0]) # extract the confidence (i.e., probability) associated # with the prediction confidence = score[0].asnumpy()[0] # filter out weak detections by requiring a minimum # confidence if confidence > confidence_threshold: # extract the index of the class label from the # detections list idx = int(class_ID) # if the class label is not a person, ignore it if CLASSES[idx] != "person": continue # compute the (x, y)-coordinates of the bounding box # for the object box = bounding_box.asnumpy() (startX, startY, endX, endY) = box.astype("int") # construct a dlib rectangle object from the bounding # box coordinates and then start the dlib correlation # tracker 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) # otherwise, we should utilize our object *trackers* rather than # object *detectors* to obtain a higher frame processing throughput 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)) # 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 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] x = [c[0] for c in to.centroids] direction_y = centroid[1] - np.mean(y) direction_x = centroid[0] - np.mean(x) to.centroids.append(centroid) cur_x = np.mean(x) cur_y = np.mean(y) x_low, x_high, y_low, y_high = W // 3, 2 * W // 3, H // 4, 3 * H // 4 cv2.line(frame, (x_low, y_low), (x_low, y_high), color=(0, 255, 0)) cv2.line(frame, (x_high, y_low), (x_high, y_high), color=(0, 255, 0)) cv2.line(frame, (x_high, y_low), (x_low, y_low), color=(0, 255, 0)) cv2.line(frame, (x_high, y_high), (x_low, y_high), color=(0, 255, 0)) # 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 delta_pixels = 10 pred_x, pred_y = cur_x + delta_pixels * np.sign(direction_x), cur_y + delta_pixels * np.sign(direction_y) if ((cur_x < x_low or cur_x > x_high) and pred_x >= x_low and pred_x >= x_high) and ((cur_y < y_low or cur_y > y_high) and pred_y >= y_low and pred_y >= y_high): totalIn += 1 to.counted = True elif cur_x >= x_low and cur_x <= x_high and cur_y >= y_low or cur_y <= y_high: totalOut += 1 to.counted = True elif cur_x >= x_low and cur_x <= x_high and cur_y >= y_low and cur_y <= y_high and (pred_x < x_low or pred_x > x_high or pred_y < y_low or pred_y > y_high): totalOut += 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(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, 255, 0), -1) # construct a tuple of information we will be displaying on the # frame info = [ ("In", totalIn), ("Out", totalOut), ("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(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() 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 not in_filename: vs.stop() # otherwise, release the video file pointer else: vs.release() # close any open windows cv2.destroyAllWindows()
def start_camera(): vs = VideoStream(src=0).start() time.sleep(2.0) fps = FPS().start() while True: frame = vs.read() print(type(frame)) 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 > 0.6: 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 faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False) DNN.setInput(faceBlob) vec = DNN.forward() preds = recognizer.predict_proba(vec)[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) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break fps.stop() cv2.destroyAllWindows() vs.stop()
def Object(): from imutils.video import VideoStream from imutils.video import FPS import numpy as np import argparse import imutils import time import cv2 import dcmotor import centrize import object_found import pandp import color 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.2, help="minimum probability to filter weak detections") args = vars(ap.parse_args()) 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(args["prototxt"], args["model"]) print("[INFO] starting video stream...") #vs = VideoStream(src=0).start() vs = VideoStream(usePiCamera=True).start() time.sleep(0.5) fps = FPS().start() while True: frame = vs.read() frame = imutils.rotate_bound(frame, 180) frame = imutils.resize(frame, width=400) (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) net.setInput(blob) detections = net.forward() reqimg = [] for i in np.arange(0, detections.shape[2]): idx = detections[0, 0, i, 1] if idx == 5: reqimg.append(i) if reqimg == []: dcmotor.left(2) time.sleep(.1) arealist = [] for i in reqimg: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") a = endX-startX b = endY-startY area = a*b arealist.append(area) arealist.sort(reverse = True) flag = 0 for i in reqimg: confidence = detections[0, 0, i, 2] if confidence > args["confidence"]: 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") a = endX-startX b = endY-startY area = a*b if area == arealist[0]: 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) centroidX =startX + a/2 #41 known width(mm),100 pixel width(px),180 distance(mm) focal = float((100*180)/41) distance = float((focal*41)/a) print("Distance using camera: {}mm".format(distance)) cv2.putText(frame, "Distance using camera: %.2fmm" % distance, (frame.shape[1] - 390, frame.shape[0] - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) centrize.Centrize(centroidX) object_found.towards_object(a) flag = 0 if (centroidX>= 190 and centroidX <= 210) and (a>=138): flag=1 if flag==1: break 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())) cv2.destroyAllWindows() vs.stop() return flag
def analyse_video(path): pridictions_datas = {} pridictions_datas['hoodie'] = 'NotAvailable' terminate = False # initialize the video stream, allow the cammera sensor to warmup, # and initialize the FPS counter print("[INFO] starting video stream...") vs = VideoStream(path).start() # video_capture = cv2.VideoCapture(path) time.sleep(0.0) fps = FPS().start() count = 0 # loop over the frames from the video stream face_Available = False while True and terminate is False: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels # frame = video_capture.read()[1] frame = vs.read() image = frame.copy() frame = imutils.resize(frame, width=1800) if not face_Available: gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 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() # 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 > confidence_range: # extract the index of the class label from the # `detections` idx = int(detections[0, 0, i, 1]) # if the predicted class label is in the set of classes # we want to ignore then skip the detection if CLASSES[idx] in IGNORE: continue # compute the (x, y)-coordinates of the bounding box for # the object 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 = "{}: {:.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) if confidence > 0.90: if checkFace(gray, frame): pridictions_datas["faceImagePath"] = str( imageName) + '.jpg' face_Available = True else: # image = image[y - 100:endY + 100, y:startX] with graph.as_default(): pridicted_labl, probability = checkAbnormalCloth( image) face_mask = False if pridicted_labl is 'Hoodies' and probability > 95 and checkFace( gray, frame) is None and face_mask: print(probability) pridictions_datas[ 'abnormal_person'] = 'Available' terminate = True if face_Available: with graph.as_default(): emotion_analyse_data = emotion_analyse(path) print(emotion_analyse_data) max_key = max(emotion_analyse_data, key=lambda k: emotion_analyse_data[k]) if max_key is 'fear' or max_key is 'angry': pridictions_datas["Identified_emotion"] = max_key pridictions_datas["bg_color"] = '#FF0000' pridictions_datas[ "Alart_msg"] = 'The Person Pridicted as Criminal' else: pridictions_datas["Identified_emotion"] = max_key pridictions_datas["bg_color"] = '#228B22' pridictions_datas["Alart_msg"] = 'Normal Person' age_gen_pridiction_lable = age_gen_analyse(StaticDir + '/face/' + str(imageName) + '.jpg') age = age_gen_pridiction_lable.split(',')[0] pridictions_datas["age"] = age pridictions_datas["gender"] = age_gen_pridiction_lable.split( ',')[1] if int(age) >= 5 and int(age) <= 15: pridictions_datas["age_range"] = 'Children' if int(age) >= 16 and int(age) <= 25: pridictions_datas["age_range"] = 'Youth' if int(age) >= 26 and int(age) <= 40: pridictions_datas["age_range"] = 'Adults' if int(age) >= 41: pridictions_datas["age_range"] = 'Seniors' wrinkle_persetage = wrinkles_analyse(StaticDir + '/face/' + str(imageName) + '.jpg') pridictions_datas["wrinkle_persetage"] = str(wrinkle_persetage) face_analyse_data = face_analyse(StaticDir + '/face/' + str(imageName) + '.jpg') terminate = True fps.update() # stop the timer and display FPS information fps.stop() cv2.destroyAllWindows() vs.stop() return pridictions_datas, emotion_analyse_data, face_analyse_data
else: frame = vs.grabImage() bool_result = True if bool_result == None: # No more frames break if roi == None: # initilize roi roi = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True) tracker = cv2.TrackerKCF_create() test = tracker.init(frame, roi) print('roi initilize correctly:', test) fps = FPS().start() # currently not being used else: # update roi bool_updated, box = tracker.update(frame) if bool_updated == True: (x, y, w, h) = [int(i) for i in box] # box holds new roi coordinates pt1 = (x, y) pt2 = (x + w, y + h) cv2.rectangle(frame, pt1, pt2, (0, 255, 0), 2) if bool_updated == False: roi = None cv2.destroyAllWindows() continue # we are not currently account for the case when update is false # this happens when the object if not longer found
def run(debug=False, sources=None, queue_size=None): # normalizing debug variable global debug_flag debug_flag = debug # standard log folder log_folder = os.path.abspath( os.path.join(os.path.dirname(os.path.realpath(__file__)), '../log/')) # standard log format log_format = '%(asctime)-8s %(levelname)-5s [%(project)s-%(version)s] user: %(user)s LOG: %(message)s' # creates a logger instance from class Logger within: # an adapter (the logging library Logger Adapter) and the verbose flag global logger logger = Logger( folder=log_folder, format=log_format, debug_flag=debug_flag, extra={ 'project': __project__, 'version': __version__, 'user': getpass.getuser() }, ) # debug flag variable logger.adapter.debug('DEBUG flags was setted as: {0}'.format(debug)) if len(sources) >= 12: logger.adapter.info( 'In this version, the maximum number of screen is fixed in 12. Sorry :(' ) sys.exit() streams = {} for index, source in enumerate(sources): stream = VideoStream(source, queue_size).start() fps = FPS().start() streams[index] = {"stream": stream, "fps": fps} # time.sleep(1.0) # while True: # aux variables text_color = (0, 255, 0) screen_height, screen_width = GetSystemMetrics(1), GetSystemMetrics(0) row = screen_height / 3 col = screen_width / 4 output_top = "press 'Q' to quit application" title = '{0}-{1} @ {2}'.format(__project__, __version__, __author__) resized = [] for index in streams: stream = streams[index]["stream"] if not stream.more(): break frame = stream.read() loss = 1 - (stream.Q.qsize() / stream.Q.maxsize) frames_ratio = '{0}/{1}'.format(stream.Q.qsize(), stream.Q.maxsize) height, width, depth = frame.shape output_bottom = "queue ratio: {0}, server delay correction: {1:.2f} ms".format( frames_ratio, loss) resized_height, resized_width = row, col cv2.putText(frame, output_bottom, (10, height - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, text_color, 2) resized.append( cv2.resize(frame, (int(resized_width), int(resized_height)))) container = np.concatenate(resized, axis=1) # add context to the frame cv2.putText(container, output_top, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.6, text_color, 2) # cv2.imshow(title, container) # server delay correction time.sleep(loss) # check if 'Q' key for 'application exit' was pressed if cv2.waitKey(22) & 0xFF == ord('q'): cv2.destroyAllWindows() sys.exit() # for index in streams: fps = streams[index]["fps"] fps.update()
def track_video(video_stream, tracker, tracker_name, video): # initialize the bounding box coordinates of the tracked object init_bb = None # initialize the FPS throughput estimator fps = None # initialize the list of tracked points, the frame counter and the coordinate deltas buffer = 800 pts = deque(maxlen=buffer) # loop over frames from the video stream while True: # grab the current frame frame = video_stream.read() frame = frame[1] if video else frame # check if the video stream has ended if frame is None: break # resize the frame and grab the frame dimensions # frame = imutils.resize(frame, width=500) (H, W) = frame.shape[:2] # check to see if we are currently tracking an object if init_bb is not None: # grab the new bounding box coordinates of the object (success, box) = tracker.update(frame) # check to see if the tracking was a success x = y = w = h = 1 if success: (x, y, w, h) = [int(v) for v in box] cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # update the FPS counter fps.update() fps.stop() x_real = translate(x, 270, 911, 0, 190) y_real = 190 - translate(y, 28, 645, -2, 190) # display tracking line center = (x + int(w / 2), y + int(h / 2)) cv2.circle(frame, center, 5, (0, 0, 255), -1) pts.appendleft(center) # loop over the set of tracked points for i in np.arange(1, len(pts)): # if either of the tracked points are None, ignore them if pts[i - 1] is None or pts[i] is None: continue # otherwise, compute the thickness of the line and draw the connecting lines thickness = int(np.sqrt(buffer / float(i + 1)) * 2.5) cv2.line(frame, pts[i - 1], pts[i], (255, 0, 0), thickness) # initialize the set of information we'll be displaying on the frame try: fps_value = fps.fps() except ZeroDivisionError: fps_value = 0 info = [ ("Tracker", tracker_name), ("Position", "(" + str(x_real) + ", " + str(y_real) + ")"), ("FPS", "{:.2f}".format(fps_value) if success else 0), ] # 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, (180, 0, 0), 2) # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the 's' key is selected, we are going to "select" a bounding # box to track if key == ord("s"): # select the bounding box of the object we want to track (make # sure you press ENTER or SPACE after selecting the ROI) init_bb = cv2.selectROI("Frame", frame, fromCenter=False, showCrosshair=True) # start OpenCV object tracker using the supplied bounding box # coordinates, then start the FPS throughput estimator as well tracker.init(frame, init_bb) fps = FPS().start() # if the `q` key was pressed, break from the loop elif key == ord("q"): break
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()
pathOut = 'video_v1.avi' fps = 25 SV = SaveVideo(name='VideoWriter', vg=video_getter, pathOut=pathIn+pathOut, fps=fps, encode_quality=95) print('[INFO] Starting saving Video...') SV.start() ct = CentroidTracker(maxDisappeared=25, maxDistance=75) trackers = [] trackers_esp32 = [] trackableObjects = {} out = 0 skipped_frames = 2 out_prev = 0 exitbool = False cpt=0; fps_count = FPS().start() while True: frame = video_getter.frame.copy() frame = imutils.resize(frame, width=500) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) (H, W) = frame.shape[:2] rects = [] #print(cpt) if cpt % skipped_frames== 0: recon = [] fotos = [] ps = [] trackers = [] trackers_esp32 = []
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 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 vino(): # 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"]) # specify the target device as the Myriad processor on the NCS net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD) # initialize the video stream, allow the cammera sensor to warmup, # and initialize the FPS counter print("[INFO] starting video stream...") vs = VideoStream(usePiCamera=True).start() time.sleep(2.0) fps = FPS().start() #curFrame = 0 # 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 time.sleep(1.0) frame = vs.read() #if curFrame == 10: # curFrame = 0 #if curFrame != 0: # curFrame += 1 # continue #else: # curFrame += 1 frame = imutils.resize(frame, width=400) # grab the frame dimensions and convert it to a blob (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 0.007843, (300, 300), 127.5) # pass the blob through the network and obtain the detections and # predictions net.setInput(blob) detections = net.forward() detection_locations = [] # 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"]: # 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 = "{}: {:.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) pt1 = (startX, startY) pt2 = (endX, endY) dict = { "name": CLASSES[idx], "probability": confidence * 100, "corners": [str(pt1), str(pt2)] } detection_locations.append(dict) # show the output frame #cv2.imshow("Frame", frame) #Write image to disk #cv2.imwrite("imageout.png", frame) #Send image and detection text to ioFog message bus img = cv2.imencode(".png", frame)[1] sendIOMessage(detection_locations, client, img) 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] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
def main(): # set the filter of the video -- VSCO! still not working maybe later # here to try the method to moving the I/O blocking operations # to a separate thread and maitaining a queue of decoded frames # in an effort to improve FPS # .read() method is a blocking I/O operation camera = PiCamera() camera.resolution = (352, 240) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(352, 240)) stream = camera.capture_continuous(rawCapture, format="bgr", use_video_port=True) camera.close() vs = PiVideoStream().start() time.sleep(2.0) fps = FPS().start() minsize = 20 caffe_model_path = "./model" threshold = [0.6, 0.7, 0.7] #initial threshold: 0.6 0.7 0.7 factor = 0.709 caffe.set_mode_cpu() #comment the next few lines? PNet = caffe.Net(caffe_model_path + "/det1.prototxt", caffe_model_path + "/det1.caffemodel", caffe.TEST) RNet = caffe.Net(caffe_model_path + "/det2.prototxt", caffe_model_path + "/det2.caffemodel", caffe.TEST) ONet = caffe.Net(caffe_model_path + "/det3.prototxt", caffe_model_path + "/det3.caffemodel", caffe.TEST) while True: start = timer() print("---------------------------------------------") frame = vs.read() #frame = imutils.resize(frame, width=400) #do we need to do the resize? # convert the frame to gray scale and restore the BGR info #grayFrame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #restore = cv2.cvtColor(grayFrame,cv2.COLOR_GRAY2BGR) #img = restore img = frame img_matlab = img.copy() tmp = img_matlab[:, :, 2].copy() img_matlab[:, :, 2] = img_matlab[:, :, 0] img_matlab[:, :, 0] = tmp # check rgb position #tic() boundingboxes, points = detect_face(img_matlab, minsize, PNet, RNet, ONet, threshold, False, factor) #toc() ## copy img to positive folder #if boundingboxes.shape[0] > 0 : # import shutil # shutil.copy(imgpath, '/home/duino/Videos/3/disdata/positive/'+os.path.split(imgpath)[1] ) #else: # import shutil # shutil.copy(imgpath, '/home/duino/Videos/3/disdata/negetive/'+os.path.split(imgpath)[1] ) img = drawBoxes(frame, boundingboxes) cv2.imshow('cam', img) if cv2.waitKey(1) & 0xFF == ord('q'): break end = timer() print("Total time:", end - start) fps.update() #When everything's done, release capture #cap.release() cv2.destroyAllWindows() vs.stop() vs.update()
def main(): button_pin = 7 GPIO.setmode(GPIO.BCM) # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(prototxt, model) # 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() fps = FPS().start() ultrasonic = UltrasonicSystem({1: [8, 25], 2: [24, 23], 3: [15, 14]}, 3) ultrasonic.add_sensors() ultrasonic_spawn = threading.Thread(target=ultrasonic.spawn_sensor_threads, daemon=True) ultrasonic_spawn.start() GPIO.add_event_detect(button_pin, GPIO.RISING) try: # 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 500 pixels frame = vs.read() frame = cv2.flip(frame, 1) # 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() # 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 > confidence_threshold: # 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 = "{}: {:.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) frame = ultrasonic.write_measurements_to_frame(frame) # show the output frame cv2.namedWindow("Frame", cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty("Frame", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) # cv2.resizeWindow("Frame", 640, 480) cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if GPIO.event_detected(button_pin): ultrasonic.stop = True time.sleep(1) break # update the FPS counter fps.update() cleanup(fps, vs) except KeyboardInterrupt: cleanup(fps, vs)
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
def main(): fps = FPS().start() #writer = None cap = cv2.VideoCapture(Input_Video) YOLOINIT() ##========================================================= ##View 1 f_num = 0 Detecting_cnt_1 = 0 RED_cnt_1 = 0 BLUE_cnt_1 = 0 initBB_1 = None tracker_1 = None while (cap.isOpened()): f_num = f_num + 1 print("F : ", f_num) (grabbed, frame) = cap.read() #cutImg = frame.copy() #cutImg = frame[300:1020, 360:1640] #frame = cutImg if f_num % 3 == 0: #====================================================== #Background Substraction #tracker 에 대해서 frame 원본이미지가 아니라, Background Substracted 된 영상에 트래커를 부착하여, 배경에 트래커가 남지 않도록 구현 gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray_frame = cv2.GaussianBlur(gray_frame, (5, 5), 0) difference = cv2.absdiff(first_gray, gray_frame) _, difference = cv2.threshold(difference, 25, 255, cv2.THRESH_BINARY) mask3 = cv2.cvtColor(difference, cv2.COLOR_GRAY2BGR) # 3 channel mask Substracted = cv2.bitwise_and(frame, mask3) #====================================================== layerOutputs, start, end = YOLO_Detect(frame) # 3.YOLO_BOX_INFO(layerOutputs,BaseConfidence,Base_threshold)) idxs, boxes, classIDs, confidences = YOLO_BOX_INFO( frame, layerOutputs, BaseConfidence, Base_threshold) # 4.검출된 화면의 X,Y 좌표 가져온다. # 검출됨 차량 수 만큼 좌표 가져옴 Vehicle_x = [] Vehicle_y = [] Vehicle_w = [] Vehicle_h = [] #차량 포인트 가져옴 Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h = Position( idxs, classIDs, boxes, Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h) #차량 포인트 그리기 Draw_Points(frame, Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h) #Parking Zone Counter #view1 (인식영역 Y축 +30 ,-30) #4개의 포인트 vertices = [[[450, 700], [460, 900], [1500, 900], [950, 700]]] #vertices = [[[70, 400], [70, 600], [1000, 600], [580, 400]]] tracker_1, initBB_1, RED_cnt_1, BLUE_cnt_1 = Passing_Counter_Zone(Vehicle_x, Vehicle_y, Vehicle_w, Vehicle_h, initBB_1, frame, tracker_1, Substracted,\ RED_cnt_1, BLUE_cnt_1, vertices) # Red_Line #cv2.line(frame, (vertices[0][0][0], vertices[0][0][1]), (vertices[0][3][0], vertices[0][3][1]), (0, 0, 255), 2) #cv2.putText(frame, "IN Cnt : " + str(RED_cnt_1), (vertices[0][0][0], vertices[0][0][1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3) # Blue_Line cv2.line(frame, (vertices[0][1][0], vertices[0][1][1]), (vertices[0][2][0], vertices[0][2][1]), (255, 0, 0), 2) cv2.putText(frame, "IN Cnt : " + str(BLUE_cnt_1), (vertices[0][1][0], vertices[0][1][1] + 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 3) # Detecting Zone pts_1 = np.array([[vertices[0][1][0] + int(2 / 3 * (vertices[0][0][0] - vertices[0][1][0])), vertices[0][0][1] + int(1 / 3 * (vertices[0][1][1] - vertices[0][0][1]))], \ [vertices[0][1][0] + int(1 / 3 * (vertices[0][0][0] - vertices[0][1][0])), vertices[0][0][1] + int(2 / 3 * (vertices[0][1][1] - vertices[0][0][1]))], \ [vertices[0][3][0] + int(2 / 3 * (vertices[0][2][0] - vertices[0][3][0])), vertices[0][3][1] + int(2 / 3 * (vertices[0][2][1] - vertices[0][3][1]))], \ [vertices[0][3][0] + int(1 / 3 * (vertices[0][2][0] - vertices[0][3][0])), vertices[0][3][1] + int(1 / 3 * (vertices[0][2][1] - vertices[0][3][1]))]], \ np.int32) cv2.polylines(frame, [pts_1], True, (0, 255, 0), 2) #프레임 레터박스 blank_image = np.zeros((64, 1920, 3), np.uint8) blank_image1 = np.zeros((600, 800, 3), np.uint8) #right blank_image2 = np.zeros((150, 600, 3), np.uint8) #right right blank_image3 = np.zeros((300, 1120, 3), np.uint8) #up blank_image4 = np.zeros((716, 400, 3), np.uint8) #left frame[0:64, 0:1920] = blank_image #frame[64:664, 1120:1920] = blank_image1 #frame[664:814, 1320:1920] = blank_image2 #frame[64:364, 0:1120] = blank_image3 #frame[364:1080, 0:400] = blank_image4 frame = cv2.resize( frame, (1280, 720), interpolation=cv2.INTER_CUBIC) #1920, 1080 -> 1280,720 #Substracted = cv2.resize(Substracted , (1280, 720), interpolation=cv2.INTER_CUBIC) fps.update() fps.stop() cv2.putText(frame, "FPS : " + "{:.2f}".format(fps.fps()), (25, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.putText(frame, "Car count : " + "{}".format(BLUE_cnt_1), (500, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.imshow("frame", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break return
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 processVideo(prototxt, model, filepath): print("[INFO] Filepath: " + filepath) print("[INFO] model: " + model) print("[INFO] prototxt: " + prototxt) outputPath = "./userapp/output.avi" skipframes = 30 conf = 0.4 # construct the argument parse and parse the arguments # ap = argparse.ArgumentParser() # ap.add_argument(prototxt) # ap.add_argument(model) # ap.add_argument(filepath) # # ap.add_argument("-o", "--output", type=str, # # help="path to optional output video file") # ap.add_argument("-c", "--confidence", type=float, default=0.4, # help="minimum probability to filter weak detections") # ap.add_argument("-s", "--skip-frames", type=int, default=30, # help="# of skip frames between detections") # args = vars(ap.parse_args()) # print("[INFO] Starting2.....") # initialize the list of class labels MobileNet SSD was trained to # detect CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(prototxt, model) # if a video path was not supplied, grab a reference to the webcam # if not args.get("input", False): # print("[INFO] starting video stream...") # vs = VideoStream(src=0).start() # time.sleep(2.0) # otherwise, grab a reference to the video file # else: # print("[INFO] opening video file...") # vs = cv2.VideoCapture(args["input"]) vs = cv2.VideoCapture(filepath) # 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 global totalUp, totalDown, netCount totalFrames = 0 # totalDown = 0 # totalUp = 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 print("[Info] Filepath is" + filepath) frame = vs.read() frame = frame[1] if filepath 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 filepath 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 outputPath is not None and writer is None: print("[INFO] loading model...after vs1") fourcc = cv2.VideoWriter_fourcc(*"MJPG") print("[INFO] loading model...after vs2 :" + outputPath) writer = cv2.VideoWriter(outputPath, fourcc, 30, (W, H), True) print("[INFO] loading model...after vs3") # 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" print("[INFO] loading model...after vs") rects = [] print("[INFO] loading model...after cv2") # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % skipframes == 0: # set the status and initialize our new set of object trackers status = "Detecting" trackers = [] # convert the frame to a blob and pass the blob through the # network and obtain the detections blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5) net.setInput(blob) detections = net.forward() # 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 requiring a minimum # confidence if confidence > conf: # extract the index of the class label from the # detections list idx = int(detections[0, 0, i, 1]) # if the class label is not a person, ignore it if CLASSES[idx] != "person": continue # compute the (x, y)-coordinates of the bounding box # for the object box = detections[0, 0, i, 3:7] * np.array([W, H, W, H]) (startX, startY, endX, endY) = box.astype("int") # construct a dlib rectangle object from the bounding # box coordinates and then start the dlib correlation # tracker 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) # otherwise, we should utilize our object *trackers* rather than # object *detectors* to obtain a higher frame processing throughput 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)) # draw a horizontal line in the center of the frame -- once an # object crosses this line we will determine whether they were # moving 'up' or 'down' cv2.line(frame, (0, H // 2), (W, H // 2), (0, 255, 255), 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 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 netCount = totalUp - totalDown yield str(netCount) + "\n" 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 netCount = totalUp - totalDown yield str(netCount) + "\n" 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(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, 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(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() 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() print("[INFO] Total Up Count: " + str(totalUp)) # if we are not using a video file, stop the camera video stream # if filepath is not None: # vs.stop() # # otherwise, release the video file pointer # else: # vs.release() # close any open windows cv2.destroyAllWindows()
def run(self): args = self.args # grab a list of all NCS devices plugged in to USB print("[INFO] finding NCS devices...") devices = mvnc.EnumerateDevices() # if no devices found, exit the script if len(devices) == 0: print("[INFO] No devices found. Please plug in a NCS") quit() # use the first device since this is a simple test script # (you'll want to modify this is using multiple NCS devices) print("[INFO] found {} devices. device0 will be used. " "opening device0...".format(len(devices))) device = mvnc.Device(devices[0]) device.OpenDevice() # open the CNN graph file print("[INFO] loading the graph file into RPi memory...") with open(args["graph"], mode="rb") as f: graph_in_memory = f.read() # load the graph into the NCS print("[INFO] allocating the graph on the NCS...") graph = device.AllocateGraph(graph_in_memory) # open a pointer to the video stream thread and allow the buffer to # start to fill, then start the FPS counter print("[INFO] starting the video stream and FPS counter...") vs = VideoStream(usePiCamera=False).start() time.sleep(1) fps = FPS().start() # loop over frames from the video file stream while True: try: # grab the frame from the threaded video stream # make a copy of the frame and resize it for display/video purposes frame = vs.read() image_for_result = frame.copy() image_for_result = cv2.resize(image_for_result, DISPLAY_DIMS) # use the NCS to acquire predictions predictions = self.predict(frame, graph) # loop over our predictions for (i, pred) in enumerate(predictions): # extract prediction data for readability (pred_class, pred_conf, pred_boxpts) = pred # filter out weak detections by ensuring the `confidence` # is greater than the minimum confidence if pred_conf > args["confidence"]: # print prediction to terminal print("[INFO] Prediction #{}: class={}, confidence={}, " "boxpoints={}".format(i, CLASSES[pred_class], pred_conf, pred_boxpts)) # check if we should show the prediction data # on the frame if args["display"] > 0: # build a label consisting of the predicted class and # associated probability label = "{}: {:.2f}%".format(CLASSES[pred_class], pred_conf * 100) # extract information from the prediction boxpoints (ptA, ptB) = (pred_boxpts[0], pred_boxpts[1]) ptA = (ptA[0] * DISP_MULTIPLIER, ptA[1] * DISP_MULTIPLIER) ptB = (ptB[0] * DISP_MULTIPLIER, ptB[1] * DISP_MULTIPLIER) (startX, startY) = (ptA[0], ptA[1]) y = startY - 15 if startY - 15 > 15 else startY + 15 # display the rectangle and label text cv2.rectangle(image_for_result, ptA, ptB, COLORS[pred_class], 2) cv2.putText(image_for_result, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 1, COLORS[pred_class], 3) if CLASSES[pred_class] == self.target_object_class: # 检测到目标,执行变道 self.message_queue.put("CHANGE_LANE") print("[WAITING] waiting for changing lane...") self.message_queue.join() if args["display"] > 0: # display the frame to the screen cv2.imshow("Output", image_for_result) # check if we should display the frame on the screen # with prediction data (you can achieve faster FPS if you # do not output to the screen) if args["display"] > 0: # display the frame to the screen cv2.imshow("Output", image_for_result) 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() # if "ctrl+c" is pressed in the terminal, break from the loop except KeyboardInterrupt: break # if there's a problem reading a frame, break gracefully except AttributeError: break # stop the FPS counter timer fps.stop() # destroy all windows if we are displaying them if args["display"] > 0: cv2.destroyAllWindows() # stop the video stream vs.stop() # clean up the graph and device graph.DeallocateGraph() device.CloseDevice() # display FPS information print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
from imutils import contours from skimage import measure from udpsocket import ThreadedUDPSocket import imutils import cv2 import time import numpy as np import argparse import math resolution = (640, 480) # length x height framerate = 60 exposure = 1000 # time in milliseconds. 10000 is normal exposure. CLIENT_IP = '192.168.1.115' CLIENT_PORT = 8049 fps = FPS() def main(args): try: with ThreadedUDPSocket(('', CLIENT_PORT)) as sock: stream = VideoStream().start() time.sleep(2) old_uid = 0 uid = 0 key = None result = () while not all(result) or key != ord(' '): while uid == old_uid: frame, uid = stream.read()
from collections import defaultdict from imutils.video import FPS import imagezmq # instantiate image_hub image_hub = imagezmq.ImageHub() image_count = 0 sender_image_counts = defaultdict(int) # dict for counts by sender first_image = True try: while True: # receive images until Ctrl-C is pressed sent_from, image = image_hub.recv_image() if first_image: fps = FPS().start( ) # start FPS timer after first image is received first_image = False fps.update() image_count += 1 # global count of all images received sender_image_counts[sent_from] += 1 # count images for each RPi name cv2.imshow(sent_from, image) # display images 1 window per sent_from cv2.waitKey(1) image_hub.send_reply(b"OK") # REP reply except (KeyboardInterrupt, SystemExit): pass # Ctrl-C was pressed to end program; FPS stats computed below except Exception as ex: print('Python error with no Exception handler:') print(ex) finally: # stop the timer and display FPS information print()
def do_GET(self): if self.path.endswith('.mjpg'): self.send_response(200) self.send_header( 'Content-type', 'multipart/x-mixed-replace; boundary=--jpgboundary') self.end_headers() frameWait = 0 fps = FPS().start() try: while True: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = capture.read() frame = imutils.resize(frame, width=640) rawFrame = frame.copy() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rects = Classifier.detector(gray, 0) for (i, rect) in enumerate(rects): # determine the facial landmarks for the face region, then # convert the facial landmark (x, y)-coordinates to a NumPy # array shape = Classifier.predictor(gray, rect) shape = face_utils.shape_to_np(shape) # convert dlib's rectangle to a OpenCV-style bounding box # [i.e., (x, y, w, h)], then draw the face bounding box (x, y, w, h) = face_utils.rect_to_bb(rect) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) # loop over the (x, y)-coordinates for the facial landmarks # and draw them on the image for (x, y) in shape: cv2.circle(frame, (x, y), 1, (0, 255, 0), -1) frameWait = 0 currentFace = rawFrame[ max(0, rect.top() - 100):min(rect.bottom() + 100, 480), max(0, rect.left() - 100):min(rect.right() + 100, 640)] cv2.imwrite("test.jpg", currentFace) validDir = Classifier._configs["ClassifierSettings"][ "NetworkPath"] + Classifier._configs[ "ClassifierSettings"]["ValidPath"] for valid in os.listdir(validDir): if valid.endswith('.jpg') or valid.endswith( '.jpeg') or valid.endswith( '.png') or valid.endswith('.gif'): if (FacenetHelpers.match( FacenetHelpers.infer( cv2.imread(validDir + valid), Classifier.graph), FacenetHelpers.infer( currentFace, Classifier.graph))): name = valid.rsplit('.', 1)[0] print("-- MATCH " + name) print("") Classifier.jumpwayClient.publishToDeviceChannel( "Warnings", { "WarningType": "CCTV", "WarningOrigin": Classifier._configs["Cameras"][0] ["ID"], "WarningValue": "RECOGNISED", "WarningMessage": name + " Detected" }) break else: print("-- NO MATCH") print("") cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2) Classifier.jumpwayClient.publishToDeviceChannel( "Warnings", { "WarningType": "CCTV", "WarningOrigin": Classifier._configs["Cameras"][0] ["ID"], "WarningValue": "INTRUDER", "WarningMessage": "INTRUDER" }) else: print("-- NO VALID ID") print("") imgRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) imgRGB = cv2.flip(imgRGB, 1) jpg = Image.fromarray(imgRGB) tmpFile = BytesIO() jpg.save(tmpFile, 'JPEG') self.wfile.write("--jpgboundary".encode()) self.send_header('Content-type', 'image/jpeg') self.send_header('Content-length', str(tmpFile.getbuffer().nbytes)) self.end_headers() self.wfile.write(tmpFile.getvalue()) #time.sleep(0.05) fps.update() frameWait = frameWait + 1 except KeyboardInterrupt: exit return if self.path.endswith('.html'): src = '<img src="http://' + Classifier._configs["Cameras"][0][ "Stream"] + ':' + str(Classifier._configs["Cameras"][0] ["StreamPort"]) + '/cam.mjpg" />' self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() self.wfile.write('<html><head></head><body>'.encode()) self.wfile.write(src.encode()) self.wfile.write('</body></html>'.encode()) return
ref: https://www.learnopencv.com/object-tracking-using-opencv-cpp-python/ """ import cv2 import sys import time from imutils.video import FPS, WebcamVideoStream #(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') major_ver, minor_ver, subminor_ver = cv2.__version__.split('.') if __name__ == '__main__' : # Track FPS fps_global = FPS().start() # Multitracker all_trackers = cv2.MultiTracker_create() # Set up tracker. # Instead of MIL, you can also use tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN'] tracker_type = tracker_types[3] trackers = [] ntrackers = 2 if int(minor_ver) < 3: for i in range(ntrackers): trackers.append(cv2.Tracker_create(tracker_type)) else:
def analyze(task, model, videopath, model_path, model_config_path, progressfunc, modelid, modelprocesstime, framerate): cap = cv2.VideoCapture(videopath) progess = 0 fileaddress = video_store + task + "_" + model + "_" + os.path.split( videopath)[1].split('.')[0] + "_" + str(framerate) + ".avi" analysisfile = task + "_" + model + "_" + os.path.split( videopath)[1].split('.')[0] + "_" + str(framerate) + ".json" fps = FPS().start() framecount = 0 utils = Utils() out = cv2.VideoWriter(fileaddress, utils.get_video_type(videopath), frames_per_second, (int(cap.get(3)), int(cap.get(4)))) if int(major_ver) < 3: print("Frames per second using video.get(cv2.cv.CV_CAP_PROP_FPS): {0}". format(cap.get(cv2.cv.CV_CAP_PROP_FPS))) else: print( "Frames per second using video.get(cv2.CAP_PROP_FPS) : {}".format( cap.get(cv2.CAP_PROP_FPS))) length = int(int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) / framerate) + 1 starttime = time.time() if cap.isOpened(): while (cap.isOpened()): ret, frame = cap.read(framecount) if framecount <= int(cap.get(cv2.CAP_PROP_FRAME_COUNT)): framecount += framerate if task == "face_detection" and model == "Haar cascade": face_cascade = cv2.CascadeClassifier(model_path) # eye_cascade = cv2.CascadeClassifier(self.model_config_path) frame = FaceDetection().haarCascade(frame, face_cascade) elif (task == "face_detection" and (model == "MTCNN" or model == "Resnet SSD")): if utils.get_video_ext(model_path) == ".caffemodel": net = cv2.dnn.readNetFromCaffe(model_config_path, model_path) else: net = cv2.dnn.readNetFromTensorflow( model_config_path, model_path) frame = FaceDetection().caffeeAndTensorModel( frame, net, fps) elif (task == "face_detection" and (model == "HOG dlib" or model == "MMOD lib")): if model == "HOG dlib": detector = dlib.get_frontal_face_detector() else: detector = dlib.cnn_face_detection_model_v1(model_path) frame = FaceDetection().dlib(model, frame, detector) elif task == "face_detection" and model == "NPD": minFace = 20 maxFace = 4000 overlap = 0.5 f = h5py.File(model_path, 'r') npdModel = { n: np.array(v) for n, v in f.get('npdModel').items() } frame = FaceDetection().npd(frame, npdModel, minFace, maxFace, overlap) elif task == "face_detection" and model == "Pytorch": from detectors import FaceBoxes DET2 = FaceBoxes(device='cuda') out.write(frame) progess = progess + 1 GLib.idle_add(progressfunc, round(((progess / length) * 100), 1)) # time.sleep(0.01) else: postAnalyze(fileaddress, analysisfile, round(time.time() - starttime, 2), utils) modelprocesstime.set_visible(True) modelprocesstime.set_text("Time : {}".format( round(time.time() - starttime, 2))) return False print("Loopend") break postAnalyze(fileaddress, analysisfile, round(time.time() - starttime, 2), utils) self.modelprocesstime.set_visible(True) self.modelprocesstime.set_text("Time : {}".format( round(time.time() - starttime, 2))) self.cap.release() print("Finish") else: print("Error opening video stream or file") dialog = openDialogMessage(Gtk.ButtonsType.OK, "Error of File", "Error opening video stream or file") response = dialog.run() if response == Gtk.ResponseType.OK: print("WARN dialog closed by clicking OK button")
length = int(video_stream.get(cv2.CAP_PROP_FRAME_COUNT)) ## Setup output video fourcc = cv2.VideoWriter_fourcc(*"mp4v") video_name = video_path.split('/')[-1].split('.')[0] out = cv2.VideoWriter(video_name + '-annotated.mp4', fourcc, video_fps, (video_width, video_height)) if args.mask is not None: mask_filter = cv2.imread(args.mask) if mask_filter.shape[:2] != (video_height, video_width): mask_filter = cv2.resize(mask_filter, (video_width, video_height)) d_dict = {} frame_id = 0 fps_counter = FPS().start() pbar = tqdm(total=length) while True: ret, frame = video_stream.read() if frame is None: break height, width, _ = frame.shape prev_time = time.time() if args.mask is not None: frame = cv2.bitwise_and(frame, mask_filter) # Preprocess image frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, (network_width, network_height), interpolation=cv2.INTER_LINEAR)
writer = None datacctv = [] ids = (1, ) W = None H = None ct = CentroidTracker(maxDisappeared=40, maxDistance=50) 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 frame = imutils.resize(frame, width=400) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if W is None or H is None: (H, W) = frame.shape[:2] if args["output"] is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(args["output"], fourcc, 30, (W, H), True)
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()