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) # 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 # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
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('Traceback error:', ex) traceback.print_exc() finally: # stop the timer and display FPS information print() print('Test Program: ', __file__) print('Total Number of Images received: {:,g}'.format(image_count)) if first_image: # never got images from any RPi sys.exit() fps.stop() print('Number of Images received from each RPi:') for RPi in sender_image_counts: print(' ', RPi, ': {:,g}'.format(sender_image_counts[RPi])) image_size = image.shape print('Size of last image received: ', image_size) uncompressed_size = image_size[0] * image_size[1] * image_size[2] print(' = {:,g} bytes'.format(uncompressed_size)) print('Elasped time: {:,.2f} seconds'.format(fps.elapsed())) print('Approximate FPS: {:.2f}'.format(fps.fps())) cv2.destroyAllWindows() sys.exit()
def run(): # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--prototxt", required=False, help="path to Caffe 'deploy' prototxt file") ap.add_argument("-m", "--model", required=True, help="path to Caffe pre-trained model") ap.add_argument("-i", "--input", type=str, help="path to optional input video file") ap.add_argument("-o", "--output", type=str, help="path to optional output video file") # confidence default 0.4 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()) # 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 net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) # if a video path was not supplied, grab a reference to the ip camera if not args.get("input", False): print("[INFO] Starting the live stream..") vs = VideoStream(config.url).start() time.sleep(2.0) # otherwise, grab a reference to the video file else: print("[INFO] Starting the video..") vs = cv2.VideoCapture(args["input"]) # initialize the video writer (we'll instantiate later if need be) writer = None # initialize the frame dimensions (we'll set them as soon as we read # the first frame from the video) W = None H = None # instantiate our centroid tracker, then initialize a list to store # each of our dlib correlation trackers, followed by a dictionary to # map each unique object ID to a TrackableObject ct = CentroidTracker(maxDisappeared=40, maxDistance=50) trackers = [] trackableObjects = {} # initialize the total number of frames processed thus far, along # with the total number of objects that have moved either up or down totalFrames = 0 totalDown = 0 totalUp = 0 x = [] empty = [] empty1 = [] # start the frames per second throughput estimator fps = FPS().start() if config.Thread: vs = thread.ThreadingClass(config.url) # 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 args.get("input", 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 args["input"] is not None and frame is None: break # resize the frame to have a maximum width of 500 pixels (the # less data we have, the faster we can process it), then convert # the frame from BGR to RGB for dlib frame = imutils.resize(frame, width=500) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # if the frame dimensions are empty, set them if W is None or H is None: (H, W) = frame.shape[:2] # if we are supposed to be writing a video to disk, initialize # the writer if args["output"] is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(args["output"], fourcc, 30, (W, H), True) # initialize the current status along with our list of bounding # box rectangles returned by either (1) our object detector or # (2) the correlation trackers status = "Waiting" rects = [] # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % args["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 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 > args["confidence"]: # 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, 0, 0), 3) cv2.putText(frame, "-Prediction border - Entrance-", (10, H - ((i * 20) + 200)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1) # 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 empty.append(totalUp) 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 empty1.append(totalDown) #print(empty1[-1]) x = [] # compute the sum of total people inside x.append(len(empty1) - len(empty)) #print("Total people inside:", x) # if the people limit exceeds over threshold, send an email alert if sum(x) >= config.Threshold: cv2.putText(frame, "-ALERT: People limit exceeded-", (10, frame.shape[0] - 80), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 2) if config.ALERT: print("[INFO] Sending email alert..") Mailer().send(config.MAIL) print("[INFO] Alert sent") 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, (255, 255, 255), 2) cv2.circle(frame, (centroid[0], centroid[1]), 4, (255, 255, 255), -1) # construct a tuple of information we will be displaying on the info = [ ("Exit", totalUp), ("Enter", totalDown), ("Status", status), ] info2 = [ ("Total people inside", x), ] # Display the output 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, 0), 2) for (i, (k, v)) in enumerate(info2): text = "{}: {}".format(k, v) cv2.putText(frame, text, (265, H - ((i * 20) + 60)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2) # Initiate a simple log to save data at end of the day if config.Log: datetimee = [datetime.datetime.now()] d = [datetimee, empty1, empty, x] export_data = zip_longest(*d, fillvalue='') with open('Log.csv', 'w', newline='') as myfile: wr = csv.writer(myfile, quoting=csv.QUOTE_ALL) wr.writerow(("End Time", "In", "Out", "Total Inside")) wr.writerows(export_data) # show the output frame cv2.imshow("Real-Time Monitoring/Analysis Window", 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() if config.Timer: # Automatic timer to stop the live stream. Set to 8 hours (28800s). t1 = time.time() num_seconds = (t1 - t0) if num_seconds > 28800: break # 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())) # # if we are not using a video file, stop the camera video stream # if not args.get("input", False): # vs.stop() # # # otherwise, release the video file pointer # else: # vs.release() # close any open windows cv2.destroyAllWindows()
class detector: def __init__(self, prototxt="mobilenet_ssd/MobileNetSSD_deploy.prototxt", model="mobilenet_ssd/MobileNetSSD_deploy.caffemodel", confidence=0.4, skipframes=10): print("Initiate detector...") self.CLASSES = [ "background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] self.confidence = confidence self.net = cv2.dnn.readNetFromCaffe(prototxt, model) #self.vs = VideoStream(src = 0, usePiCamera = True).start() self.vs = VideoStream(src=0).start() self.W = None self.H = None self.ct = CentroidTracker(maxDisappeared=40, maxDistance=50) self.trackers = [] self.trackableObjects = {} self.totalFrames = 0 self.skipframes = skipframes self.fps = FPS().start() def main(self): while True: frame = self.vs.read() frame = frame[1] # frame = frame[1] if args.get("input", False) else frame frame = imutils.resize(frame, width=250) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if self.W is None or self.H is None: (self.H, self.W) = frame.shape[:2] status = "Waiting" rects = [] if self.totalFrames % self.skipframes == 0: status = "Detecting" self.trackers = [] blob = cv2.dnn.blobFromImage(frame, 0.007843, (self.W, self.H), 127.5) self.net.setInput(blob) detections = self.net.forward() for i in np.arange(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > self.confidence: idx = int(detections[0, 0, i, 1]) if self.CLASSES[idx] != "person": continue box = detections[0, 0, i, 3:7] * np.array( [self.W, self.H, self.W, self.H]) (startX, startY, endX, endY) = box.astype("int") tracker = dlib.correlation_tracker() rect = dlib.rectangle(startX, startY, endX, endY) tracker.start_track(rgb, rect) self.trackers.append(tracker) else: for tracker in self.trackers: status = "Tracking" tracker.update(rgb) pos = tracker.get_position() startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) rects.append((startX, startY, endX, endY)) cv2.line(frame, (0, self.H // 2), (self.W, self.H // 2), (0, 255, 255), 2) objects = self.ct.update(rects) for (objectID, centroid) in objects.items(): to = self.trackableObjects.get(objectID, None) if to is None: to = TrackableObject(objectID, centroid) else: y = [c[1] for c in to.centroids] direction = centroid[1] - np.mean(y) to.centroids.append(centroid) if not to.counted: if direction < 0 and centroid[1] < self.H // 2: totalUp += 1 to.counted = True elif direction > 0 and centroid[1] > self.H // 2: totalDown += 1 to.counted = True self.trackableObjects[objectID] = to 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) info = [ ("Up", totalUp), ("Down", totalDown), ("Status", status), ] for (i, (k, v)) in enumerate(info): text = "{}: {}".format(k, v) cv2.putText(frame, text, (10, self.H - ((i * 20) + 20)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) # show the output frame cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # increment the total number of frames processed thus far and # then update the FPS counter self.totalFrames += 1 self.fps.update() # stop the timer and display FPS information self.fps.stop() print("[INFO] elapsed time: {:.2f}".format(self.fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(self.fps.fps())) self.vs.stop() cv2.destroyAllWindows()
def video_check(self): detector = cv2.dnn.readNetFromCaffe( 'face_detection_model/deploy.prototxt', 'face_detection_model/res10_300x300_ssd_iter_140000.caffemodel') #summary # load our serialized face embedding model from disk print("[INFO] loading face recognizer...") embedder = cv2.dnn.readNetFromTorch('openface_nn4.small2.v1.t7') # load the actual face recognition model along with the label encoder recognizer = pickle.loads( open('output/recognizer.pickle', "rb").read()) le = pickle.loads(open('output/le.pickle', "rb").read()) # initialize the video stream, then allow the camera sensor to warm up print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) #run check for only 15seconds and then stop timeout = time.time() + 5 # start the FPS throughput estimator fps = FPS().start() # loop over frames from the video file stream real_user_list = [] while True: #run check for only 15seconds and then stop if time.time() > timeout: cv2.destroyWindow("Frame") break # grab the frame from the threaded video stream frame = vs.read() # resize the frame to have a width of 600 pixels (while # maintaining the aspect ratio), and then grab the image # dimensions frame = imutils.resize(frame, width=800, height=200) (h, w) = frame.shape[:2] # construct a blob from the image imageBlob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) # apply OpenCV's deep learning-based face detector to localize # faces in the input image detector.setInput(imageBlob) detections = detector.forward() #TODO: if 2 faces are detected alert the user of a warning # loop over the detections for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with # the prediction confidence = detections[0, 0, i, 2] # filter out weak detections if confidence > 0.5: # compute the (x, y)-coordinates of the bounding box for # the face box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # extract the face ROI face = frame[startY:endY, startX:endX] (fH, fW) = face.shape[:2] # ensure the face width and height are sufficiently large if fW < 20 or fH < 20: continue # construct a blob for the face ROI, then pass the blob # through our face embedding model to obtain the 128-d # quantification of the face faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False) embedder.setInput(faceBlob) vec = embedder.forward() # perform classification to recognize the face preds = recognizer.predict_proba(vec)[0] j = np.argmax(preds) proba = preds[j] name = le.classes_[j] # # draw the bounding box of the face along with the # # associated probability # text = "{}: {:.2f}%".format(name, proba * 100) # y = startY - 10 if startY - 10 > 10 else startY + 10 # cv2.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) #TODO: Handle if 2 faces are given. #Decision boundary if (name == 'unknown') or (proba * 100) < 50: print("Fraud detected") real_user_list.append(name) else: #cv2.destroyWindow("Frame") real_user_list.append(name) break # update the FPS counter fps.update() # 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 # 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() print(real_user_list) try: Counter(real_user_list).most_common(1)[0][0] == 'unknown' except IndexError: if self.countter != 0: messagebox._show( "Verification Info!", "Face Id match failed! You have {} trials left".format( self.countter)) self.countter = self.countter - 1 self.video_check() else: messagebox._show( "Verification Info!", "Face Id match failed! You cannot withdraw at this time, try again later" ) self.begin_page() self.countter = 2 else: if Counter(real_user_list).most_common(1)[0][0] == 'unknown': if self.countter != 0: messagebox._show( "Verification Info!", "Face Id match failed! You have {} trials left".format( self.countter)) self.countter = self.countter - 1 self.video_check() else: messagebox._show( "Verification Info!", "Face Id match failed! You cannot withdraw at this time, try again later" ) self.begin_page() self.countter = 2 else: self.real_user = int( Counter(real_user_list).most_common(1)[0][0]) messagebox._show("Verification Info!", "Face Id match!") self.password_verification()
def tracker_speed(self): # construct the argument parser and parse the arguments #ap = argparse.ArgumentParser() #ap.add_argument("-c", "--conf", required=True, # help="Path to the input configuration file") #ap.add_argument("-i", "--input", required=True, # help="Path to the input video file") #args = vars(ap.parse_args()) # load the configuration file #conf = Conf(args["conf"]) conteudo = open(conf_path).read() conf = json.loads(conteudo) # 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"] # check to see if the Dropbox should be used if conf["use_dropbox"]: # connect to dropbox and start the session authorization process client = dropbox.Dropbox(conf["dropbox_access_token"]) print("[SUCCESS] dropbox account linked") # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(conf["prototxt_path"], conf["model_path"]) #net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD) # initialize the video stream and allow the camera sensor to warmup print("[INFO] warming up camera...") #vs = VideoStream(src=0).start() vs = cv2.VideoCapture(data_path) tm.sleep(2.0) # initialize the frame dimensions (we'll set them as soon as we read # the first frame from the video) H = None W = None # instantiate our centroid tracker, then initialize a list to store # each of our dlib correlation trackers, followed by a dictionary to # map each unique object ID to a TrackableObject ct = CentroidTracker(maxDisappeared=conf["max_disappear"], maxDistance=conf["max_distance"]) trackers = [] trackableObjects = {} # keep the count of total number of frames totalFrames = 0 # initialize the log file logFile = None # initialize the list of various points used to calculate the avg of # the vehicle speed points = [("A", "B"), ("B", "C"), ("C", "D")] # start the frames per second throughput estimator fps = FPS().start() # loop over the frames of the stream while True: # grab the next frame from the stream, store the current # timestamp, and store the new date ret, frame = vs.read() ts = datetime.now() newDate = ts.strftime("%m-%d-%y") # check if the frame is None, if so, break out of the loop if frame is None: break # if the log file has not been created or opened if logFile is None: # build the log file path and create/open the log file logPath = os.path.join(conf["output_path"], conf["csv_name"]) logFile = open(logPath, mode="a") # set the file pointer to end of the file pos = logFile.seek(0, os.SEEK_END) # if we are using dropbox and this is a empty log file then # write the column headings if conf["use_dropbox"] and pos == 0: logFile.write("Year,Month,Day,Time,Speed (in KMPH),ImageID\n") # otherwise, we are not using dropbox and this is a empty log # file then write the column headings elif pos == 0: logFile.write("Year,Month,Day,Time (in KMPH),Speed\n") # resize the frame frame = imutils.resize(frame, width=conf["frame_width"]) 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] meterPerPixel = conf["distance"] / W # initialize our list of bounding box rectangles returned by # either (1) our object detector or (2) the correlation trackers rects = [] # check to see if we should run a more computationally expensive # object detection method to aid our tracker if totalFrames % conf["track_object"] == 0: # initialize our new set of object trackers trackers = [] # convert the frame to a blob and pass the blob through the # network and obtain the detections blob = cv2.dnn.blobFromImage(frame, size=(300, 300), ddepth=cv2.CV_8U) net.setInput(blob, scalefactor=1.0/127.5, mean=[127.5, 127.5, 127.5]) 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 > conf["confidence"]: # 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 car, ignore it if CLASSES[idx] != "car": continue # if the class label is not a motorbike, ignore it #if CLASSES[idx] != "motorbike": # 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: # 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, if there is a trackable object and its speed has # not yet been estimated then estimate it elif not to.estimated: # check if the direction of the object has been set, if # not, calculate it, and set it if to.direction is None: y = [c[0] for c in to.centroids] direction = centroid[0] - np.mean(y) to.direction = direction # if the direction is positive (indicating the object # is moving from left to right) if to.direction > 0: # check to see if timestamp has been noted for # point A if to.timestamp["A"] == 0 : # if the centroid's x-coordinate is greater than # the corresponding point then set the timestamp # as current timestamp and set the position as the # centroid's x-coordinate if centroid[0] > conf["speed_estimation_zone"]["A"]: to.timestamp["A"] = ts to.position["A"] = centroid[0] # check to see if timestamp has been noted for # point B elif to.timestamp["B"] == 0: # if the centroid's x-coordinate is greater than # the corresponding point then set the timestamp # as current timestamp and set the position as the # centroid's x-coordinate if centroid[0] > conf["speed_estimation_zone"]["B"]: to.timestamp["B"] = ts to.position["B"] = centroid[0] # check to see if timestamp has been noted for # point C elif to.timestamp["C"] == 0: # if the centroid's x-coordinate is greater than # the corresponding point then set the timestamp # as current timestamp and set the position as the # centroid's x-coordinate if centroid[0] > conf["speed_estimation_zone"]["C"]: to.timestamp["C"] = ts to.position["C"] = centroid[0] # check to see if timestamp has been noted for # point D elif to.timestamp["D"] == 0: # if the centroid's x-coordinate is greater than # the corresponding point then set the timestamp # as current timestamp, set the position as the # centroid's x-coordinate, and set the last point # flag as True if centroid[0] > conf["speed_estimation_zone"]["D"]: to.timestamp["D"] = ts to.position["D"] = centroid[0] to.lastPoint = True # if the direction is negative (indicating the object # is moving from right to left) elif to.direction < 0: # check to see if timestamp has been noted for # point D if to.timestamp["D"] == 0 : # if the centroid's x-coordinate is lesser than # the corresponding point then set the timestamp # as current timestamp and set the position as the # centroid's x-coordinate if centroid[0] < conf["speed_estimation_zone"]["D"]: to.timestamp["D"] = ts to.position["D"] = centroid[0] # check to see if timestamp has been noted for # point C elif to.timestamp["C"] == 0: # if the centroid's x-coordinate is lesser than # the corresponding point then set the timestamp # as current timestamp and set the position as the # centroid's x-coordinate if centroid[0] < conf["speed_estimation_zone"]["C"]: to.timestamp["C"] = ts to.position["C"] = centroid[0] # check to see if timestamp has been noted for # point B elif to.timestamp["B"] == 0: # if the centroid's x-coordinate is lesser than # the corresponding point then set the timestamp # as current timestamp and set the position as the # centroid's x-coordinate if centroid[0] < conf["speed_estimation_zone"]["B"]: to.timestamp["B"] = ts to.position["B"] = centroid[0] # check to see if timestamp has been noted for # point A elif to.timestamp["A"] == 0: # if the centroid's x-coordinate is lesser than # the corresponding point then set the timestamp # as current timestamp, set the position as the # centroid's x-coordinate, and set the last point # flag as True if centroid[0] < conf["speed_estimation_zone"]["A"]: to.timestamp["A"] = ts to.position["A"] = centroid[0] to.lastPoint = True # check to see if the vehicle is past the last point and # the vehicle's speed has not yet been estimated, if yes, # then calculate the vehicle speed and log it if it's # over the limit if to.lastPoint and not to.estimated: # initialize the list of estimated speeds estimatedSpeeds = [] # loop over all the pairs of points and estimate the # vehicle speed for (i, j) in points: # calculate the distance in pixels d = to.position[j] - to.position[i] distanceInPixels = abs(d) # check if the distance in pixels is zero, if so, # skip this iteration if distanceInPixels == 0: continue # calculate the time in hours t = to.timestamp[j] - to.timestamp[i] timeInSeconds = abs(t.total_seconds()) timeInHours = timeInSeconds / (60 * 60) # calculate distance in kilometers and append the # calculated speed to the list distanceInMeters = distanceInPixels * meterPerPixel distanceInKM = distanceInMeters / 1000 estimatedSpeeds.append(distanceInKM / timeInHours) # calculate the average speed to.calculate_speed(estimatedSpeeds) # set the object as estimated to.estimated = True print("[INFO] Speed of the vehicle that just passed"\ " is: {:.2f} KMPH".format(to.speedKMPH)) #str = ("{:.2f}".format(to.speedKMPH)) self.set_velocidade(int(to.speedKMPH)) # 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) # check if the object has not been logged if not to.logged: # check if the object's speed has been estimated and it # is higher than the speed limit if to.estimated and to.speedKMPH > conf["speed_limit"]: # set the current year, month, day, and time year = ts.strftime("%Y") month = ts.strftime("%m") day = ts.strftime("%d") time = ts.strftime("%H:%M:%S") # check if dropbox is to be used to store the vehicle # image if conf["use_dropbox"]: # initialize the image id, and the temporary file imageID = ts.strftime("%H%M%S%f") tempFile = TempFile() cv2.imwrite(tempFile.path, frame) # create a thread to upload the file to dropbox # and start it t = Thread(target=upload_file, args=(tempFile, client, imageID,)) t.start() # log the event in the log file info = "{},{},{},{},{},{}\n".format(year, month, day, time, to.speedKMPH, imageID) logFile.write(info) # otherwise, we are not uploading vehicle images to # dropbox else: # log the event in the log file info = "{},{},{},{},{}\n".format(year, month, day, time, to.speedKMPH) logFile.write(info) # set the object has logged to.logged = True # if the *display* flag is set, then display the current frame # to the screen and record if a user presses a key if conf["display"]: cv2.imshow("frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key is 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 if the log file object exists, if it does, then close it if logFile is not None: logFile.close() # close any open windows cv2.destroyAllWindows() # clean up print("[INFO] cleaning up...") vs.release()
class LiveStreamer(object): """ """ def __init__(self, cam_id): self.win_name = 'Pi Live Streaming' self.cam, self.cap = init_pi_cam(self.win_name) self.fps = FPS().start() self.in_q = Queue() self.out_q = Queue() self.stopped = False self.threads = [] def start(self): th = Thread(target=self.frame_preprocess) th.start() self.threads.append(th) th = Thread(target=self.frame_process) th.start() self.threads.append(th) th = Thread(target=self.stream) th.start() self.threads.append(th) def frame_preprocess(self): for pi_frame in self.cam.capture_continuous(self.cap, format="bgr", use_video_port = True): if self.stopped is True: break frame = pi_frame.array self.in_q.put(frame) if frame is None: break self.stopped = True def frame_process(self): while True: if self.stopped is True: break frame = self.in_q.get() if frame is None: break self.fps.update() self.out_q.put(frame) self.in_q.task_done() self.stopped = True def stream(self): while True: frame = self.out_q.get() if frame is None: break cv2.imshow(self.win_name, frame) self.out_q.task_done() self.stopped = True def stop(self): self.stopped = True self.in_q.put(None) self.out_q.put(None) for th in self.threads: th.join() self.fps.stop() print("Elapsed = {:.2f} sec".format(self.fps.elapsed())) print("Frame Rate = {:.2f} fps".format(self.fps.fps())) self.cam.close()
def evaluateallframescreatesubmission(base_dir, filename, outputsubmissionfilepath, outputfile="./output_video1.mp4"): Final_array = np.load(base_dir / filename, allow_pickle=True, mmap_mode='r') data_array = Final_array['arr_0'] array_len = len(data_array) # 20, 200 frames in one file, downsample by 10 print("Final_array lenth:", array_len) print("Final_array type:", type(data_array)) # numpy.ndarray objects = metrics_pb2.Objects() #submission objects frame_width = 1920 frame_height = 1280 out = cv2.VideoWriter(outputfile, cv2.VideoWriter_fourcc('M', 'P', '4', 'V'), 2, (frame_width, frame_height)) fps = FPS().start() wod_latency_submission.initialize_model() required_field = wod_latency_submission.DATA_FIELDS print(required_field) for frameid in range(array_len): #frameid = 5 print("frameid:", frameid) # {'key':key, 'context_name':context_name, 'framedict':framedict} convertedframesdict = data_array[frameid] frame_timestamp_micros = convertedframesdict['key'] context_name = convertedframesdict['context_name'] framedict = convertedframesdict['framedict'] # 10017090168044687777_6380_000_6400_000 #print('context_name:', context_name) #print('frame_timestamp_micros:', frame_timestamp_micros) # 1550083467346370 #result = wod_latency_submission.run_model(framedict[required_field[0]], framedict[required_field[1]]) #result = wod_latency_submission.run_model(**framedict) Front_image = framedict[required_field[0]] start_time = time.time() result = wod_latency_submission.run_model(Front_image) end_time = time.time() elapsed_time = end_time - start_time print('Inference time: ' + str(elapsed_time) + 's') #print(result) createsubmisionobject(objects, result['boxes'], result['classes'], result['scores'], context_name, frame_timestamp_micros) #Save the original image #output_path = "./test.png" #visualization_util.save_image_array_as_png(Front_image, output_path) image_np_with_detections = Front_image.copy() visualization_util.visualize_boxes_and_labels_on_image_array( image_np_with_detections, result['boxes'], result['classes'], result['scores'], category_index, use_normalized_coordinates=False, max_boxes_to_draw=200, min_score_thresh=Threshold, agnostic_mode=False) #visualization_util.save_image_array_as_png(image_np_with_detections, outputfile) name = './Test_data/frame' + str(frameid) + '.jpg' #print ('Creating\...' + name) #cv2.imwrite(name, image_np_with_detections) #write to image folder fps.update() out.write(image_np_with_detections) # 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())) out.release() submission = submission_pb2.Submission() submission.task = submission_pb2.Submission.DETECTION_2D submission.account_name = '*****@*****.**' submission.authors.append('Kaikai Liu') submission.affiliation = 'None' submission.unique_method_name = 'torchvisionfaster' submission.description = 'none' submission.method_link = "empty method" submission.sensor_type = submission_pb2.Submission.CAMERA_ALL submission.number_past_frames_exclude_current = 0 submission.number_future_frames_exclude_current = 0 submission.inference_results.CopyFrom(objects) f = open(outputsubmissionfilepath, 'wb') #output submission file f.write(submission.SerializeToString()) f.close() now = datetime.datetime.now() print("Finished validation, current date and time : ") print(now.strftime("%Y-%m-%d %H:%M:%S"))
def main(): video = cv2.VideoCapture(input_video) frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)) analyzer = Analyzer() engine = Engine(frame_width, frame_height, confidence_threshold=confidence_threshold, face_confidence_threshold=face_confidence_threshold) writer = None frames_processed = 0 fps = FPS().start() while True: (grabbed, frame) = video.read() if frame is None: # end of video break engine.process(frame) objs = engine.get_objects() faces = engine.get_faces() analyzer.add_frame(objs, faces) for obj in objs: draw_box_text(frame, obj.rect, obj.label) for face in faces: draw_face(frame, face.rect) if frame_width is None or frame_height is None: (frame_height, frame_width) = frame.shape[:2] if output_video is not None and writer is None: fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(output_video, fourcc, 30, (frame_width, frame_height), True) if frames_processed % report_frame_period == 0: progress = frames_processed / frame_count print("frame {}/{} ({:.0%})".format(frames_processed, frame_count, progress)) if report_progress_files: file = open(progress_filename, 'w') file.write("{}".format(int(progress * 100))) file.close() # info = [ # ("Status", status) # ] # for (i, (k, v)) in enumerate(info): # text = "{}: {}".format(k, v) # cv2.putText(frame, text, (10, frame_height - ((i * 20) + 20)), # cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2) if writer is not None: writer.write(frame) if draw: cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break frames_processed += 1 fps.update() fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) if writer is not None: writer.release() video.release() if draw: cv2.destroyAllWindows() if output_analysis: analyzer.analyze() serialized = analyzer.serialize() file = open(output_analysis, 'w') file.write(serialized) file.close() if report_progress_files: os.remove(progress_filename)
def helmet(video): print("Working Wait It may Take Some Minutes! ") FILE_OUTPUT = app.config['Third_Eye_Out']+video # initialize the list of class labels MobileNet SSD was trained to detect # generate a set of bounding box colors for each class CLASSES = ['background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'] #CLASSES = ['motorbike', 'person'] COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) # load our serialized model from disk print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe('helmetModel/MobileNetSSD_deploy.prototxt.txt', 'helmetModel/MobileNetSSD_deploy.caffemodel') print('Loading helmet model...') loaded_model = load_model('helmetModel/new_helmet_model.h5') loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) # initialize the video stream, print("[INFO] starting video stream...") # Loading the video file videopath=os.path.join(app.config['Third_Eye_Media'],'%s'%video) print(videopath) cap = cv2.VideoCapture(videopath) currentFrame = 0 # Get current width of frame width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float # Get current height of frame height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'X264') out = cv2.VideoWriter(FILE_OUTPUT,fourcc, 20.0, (int(width),int(height))) # time.sleep(2.0) # Starting the FPS calculation fps = FPS().start() # loop over the frames from the video stream # i = True while True: # i = not i # if i==True: try: # grab the frame from the threaded video stream and resize it # to have a maxm width and height of 600 pixels ret, frame = cap.read() # resizing the images frame = imutils.resize(frame, width=600, height=600) # grab the frame dimensions and convert it to a blob (h, w) = frame.shape[:2] # Resizing to a fixed 300x300 pixels and normalizing it. # Creating the blob from image to give input to the Caffe Model blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) # pass the blob through the network and obtain the detections and predictions net.setInput(blob) detections = net.forward() # getting the detections from the network persons = [] person_roi = [] motorbi = [] # loop over the detections for i in np.arange(0, detections.shape[2]): # extract the confidence associated with the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the confidence # is greater than minimum confidence if confidence > 0.5: # extract index of class label from the detections idx = int(detections[0, 0, i, 1]) if idx == 15: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") # roi = box[startX:endX, startY:endY/4] # person_roi.append(roi) persons.append((startX, startY, endX, endY)) if idx == 14: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") motorbi.append((startX, startY, endX, endY)) xsdiff = 0 xediff = 0 ysdiff = 0 yediff = 0 p = () for i in motorbi: mi = float("Inf") for j in range(len(persons)): xsdiff = abs(i[0] - persons[j][0]) xediff = abs(i[2] - persons[j][2]) ysdiff = abs(i[1] - persons[j][1]) yediff = abs(i[3] - persons[j][3]) if (xsdiff+xediff+ysdiff+yediff) < mi: mi = xsdiff+xediff+ysdiff+yediff p = persons[j] # r = person_roi[j] if len(p) != 0: # display the prediction label = "{}".format(CLASSES[14]) # print("[INFO] {}".format(label)) # cv2.rectangle(frame, (i[0], i[1]), (i[2], i[3]), COLORS[14], 2) #Vehicle Body y = i[1] - 15 if i[1] - 15 > 15 else i[1] + 15 # cv2.putText(frame, label, (i[0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[14], 2) label = "{}".format(CLASSES[15]) # print("[INFO] {}".format(label)) # cv2.rectangle(frame, (p[0], p[1]), (p[2], p[3]), COLORS[15], 2) #Person Body y = p[1] - 15 if p[1] - 15 > 15 else p[1] + 15 roi = frame[p[1]:p[1]+(p[3]-p[1])//4, p[0]:p[2]] # print(roi) if len(roi) != 0: img_array = cv2.resize(roi, (50,50)) gray_img = cv2.cvtColor(img_array, cv2.COLOR_BGR2GRAY) img = np.array(gray_img).reshape(1, 50, 50, 1) img = img/255.0 # cv2.imshow("img",img) # print(img) prediction = loaded_model.predict_proba([img]) cv2.rectangle(frame, (p[0], p[1]), (p[0]+(p[2]-p[0]), p[1]+(p[3]-p[1])//4), [0,0,255], 2) if(round(prediction[0][0],2))<=.90: cv2.putText(frame, "No Helmet", (p[0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0,0,255], 2) # print(round(prediction[0][0],2)) # cv2.putText(frame, "Helmet", (p[0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[0], 2) except: pass cv2.imshow('Frame', frame) # Displaying the frame # Saves for video out.write(frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): # if 'q' key is pressed, break from the loop break # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() cap.release() # Closing the video stream out.release() return "Video Saved!"
def main_process(self): Base={ "max_disappear": 30, "max_distance": 200, "track_object": 4, "confidence": 0.4, "frame_height": 400, "line_point" : 125, "display": "true", "model_path": "MobileNetSSD_deploy.caffemodel", "prototxt_path": "MobileNetSSD_deploy.prototxt", "output_path": "output", "csv_name": "log.csv" } CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] print("[INFO] loading model...") net = cv2.dnn.readNetFromCaffe(Base["prototxt_path"], Base["model_path"]) print("[INFO] warming up camera...") vs = cv2.VideoCapture(self.filename) H = None W = None ct = CentroidTracker(maxDisappeared=Base["max_disappear"], maxDistance=Base["max_distance"]) trackers = [] trackableObjects = {} totalFrames = 0 logFile = None points = [("A", "B"), ("B", "C"), ("C", "D")] fps = FPS().start() while True: ret, frame = vs.read() ts = datetime.now() newDate = ts.strftime("%m-%d-%y") minut=ts.minute if frame is None: break frame = imutils.resize(frame, height=Base["frame_height"]) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if W is None or H is None: (H, W) = frame.shape[:2] rects = [] if totalFrames % Base["track_object"] == 0: trackers = [] blob = cv2.dnn.blobFromImage(frame, size=(300, 300), ddepth=cv2.CV_8U) net.setInput(blob, scalefactor=1.0/127.5, mean=[127.5, 127.5, 127.5]) detections = net.forward() # loop over the detections for i in np.arange(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > Base["confidence"]: idx = int(detections[0, 0, i, 1]) if CLASSES[idx] != "car": if CLASSES[idx] != "bus": if CLASSES[idx] != "motorbike": continue box = detections[0, 0, i, 3:7] * np.array([W, H, W, H]) (startX, startY, endX, endY) = box.astype("int") tracker = dlib.correlation_tracker() rect = dlib.rectangle(int(startX), int(startY), int(endX), int(endY)) tracker.start_track(rgb, rect) cv2.rectangle(frame, (startX, startY), (endX, endY), (0,225,0), 4) trackers.append(tracker) else: for tracker in trackers: tracker.update(rgb) pos = tracker.get_position() startX = int(pos.left()) startY = int(pos.top()) endX = int(pos.right()) endY = int(pos.bottom()) cv2.rectangle(frame, (startX, startY), (endX, endY), (0,225,0), 4) rects.append((startX, startY, endX, endY)) objects = ct.update(rects) for (objectID, centroid) in objects.items(): to = trackableObjects.get(objectID, None) if to is None: to = TrackableObject(objectID, centroid) elif not to.estimated: y = [c[1] for c in to.centroids] direction = centroid[1] - np.mean(y) to.direction = direction if(to.direction>0): tet = "down" cv2.putText(frame, tet, (centroid[0] - 10, centroid[1] - 20) , cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) if minut%2==0: if not to.belowline: if(centroid[1] < self.line_point): to.belowline = "F" else: to.belowline = "T" else: if(to.belowline == "F" and centroid[1] > self.line_point): if not to.savethefile: #crop = frame[startX:endX, startY:endY] cv2.imwrite('output/violation'+str(self.saveno)+'.jpg', frame) to.savethefile = 1 self.saveno += 1 cv2.circle(frame, (centroid[0]+10, centroid[1]), 4, (0, 0, 255), -1) else: if to.belowline: to.belowline = None elif(to.direction<0): tet = "up" cv2.putText(frame, tet, (centroid[0] - 10, centroid[1] - 20) , cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) elif(to.direction==0): tet = "stationary" cv2.putText(frame, tet, (centroid[0] - 10, centroid[1] - 20) , cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) trackableObjects[objectID] = to 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) if minut%2==0: cv2.line(frame, (0, self.line_point), (2000, self.line_point), (0,0,255), 4) else: cv2.line(frame, (0, self.line_point), (2000, self.line_point), (0,255,0), 4) if Base["display"]=="true": cv2.imshow("frame", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break totalFrames += 1 fps.update() fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() vs.release()
def main(): pts1 = np.array([[119, 1190], [1986, 1130], [163, 2958], [1962, 3033]]) pts2 = np.array([[109, 859], [2028, 751], [218, 2619], [1971, 2685]]) pts3 = np.array([[44, 907], [1901, 859], [57, 2554], [1824, 2784]]) pts4 = np.array([[66, 1024], [1938, 963], [118, 2767], [1881, 2877]]) vs1 = WebcamVideoStream(src=gstreamer_pipeline(sensor_id=0), device=cv2.CAP_GSTREAMER).start() vs2 = WebcamVideoStream(src=gstreamer_pipeline(sensor_id=1), device=cv2.CAP_GSTREAMER).start() vs3 = WebcamVideoStream(src=gstreamer_pipeline(sensor_id=2), device=cv2.CAP_GSTREAMER).start() vs4 = WebcamVideoStream(src=gstreamer_pipeline(sensor_id=3), device=cv2.CAP_GSTREAMER).start() rect1 = order_points(pts1) rect2 = order_points(pts2) rect3 = order_points(pts3) rect4 = order_points(pts4) dst1, maxWidth1, maxHeight1 = four_point_transform(rect1) dst2, maxWidth2, maxHeight2 = four_point_transform(rect2) dst3, maxWidth3, maxHeight3 = four_point_transform(rect3) dst4, maxWidth4, maxHeight4 = four_point_transform(rect4) M1 = cv2.getPerspectiveTransform(rect1, dst1) M2 = cv2.getPerspectiveTransform(rect2, dst2) M3 = cv2.getPerspectiveTransform(rect3, dst3) M4 = cv2.getPerspectiveTransform(rect4, dst4) print("[INFO] sampling THREADED frames from webcam...") fps = FPS().start() while True: frame1 = vs1.read() frame2 = vs2.read() frame3 = vs3.read() frame4 = vs4.read() warped1 = cv2.warpPerspective(frame1, M1, (maxWidth1, maxHeight1)) warped2 = cv2.warpPerspective(frame2, M2, (maxWidth2, maxHeight2)) warped3 = cv2.warpPerspective(frame3, M3, (maxWidth3, maxHeight3)) warped4 = cv2.warpPerspective(frame4, M4, (maxWidth4, maxHeight4)) cv2.imshow('frame1', warped1) cv2.imshow('frame2', warped2) cv2.imshow('frame3', warped3) cv2.imshow('frame4', warped4) if cv2.waitKey(1) & 0xFF == ord('q'): break 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())) cv2.destroyAllWindows() vs1.stop() vs2.stop() vs3.stop() vs4.stop()
def main(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('-t', '--threshold', type=float, default=0.5, help='Score threshold for detected objects.') parser.add_argument('-c', '--count', type=int, default=5, help='Number of times to run inference') parser.add_argument('-tpu', '--enable-tpu', action='store_true', help='Whether TPU is enabled or not') parser.add_argument('-objects', '--detect-objects', type=str, default="bird") parser.add_argument('-debug', '--enable-debug', action='store_true', help='Whether Debug is enabled or not - Webcamera viewed is displayed when in this mode') args = parser.parse_args() objects_to_detect = args.detect_objects if args.enable_tpu: model_dir = "/output/edgetpu/" else: "model_dir = "/output/tflite/" labels = load_labels(args.model) interpreter = make_interpreter(args.model, args.enable_tpu) interpreter.allocate_tensors() # begin detect video # 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() # enable circular stream cameraCircularStream = enableCircularCameraRecording(piVideoStream) time.sleep(1) while True: t0 = cv2.getTickCount() frame = piVideoStream.read() fps.update() image_rgb_np = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # when passing the size to the method we reverse the tuple scale = detect.set_input(interpreter, image_rgb_np.shape[:2][::-1], lambda size: cv2.resize(image_rgb_np,size, interpolation=cv2.INTER_AREA)) interpreter.invoke() objs = detect.get_output(interpreter, args.threshold, scale) # we only draw bounding boxes and detection labels in the # frame if we are in debug mode if objs and args.enable_debug: draw_objects(frame, objs, objects_to_detect, labels) # we only record to video file if not in debug mode if not args.enable_debug and detected_object(objs, objects_to_detect, labels): log_detected_objects(objs, objects_to_detect, labels) # record 20 s video clip - it will freeze main thread recordVideoFromCamera(piVideoStream,cameraCircularStream) # in debug mode we show the object detection boxes if args.enable_debug: cv2.imshow('frame', frame) 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 read_live_barcode(detection_threshold=50): """Live read the barcode and returns data""" ##detection_threshold = 50 # 1 sec ##detection_threshold = 100 # 2 sec detection_time = 'no detection' # initialize the video stream and allow the camera sensor to warm up print("[INFO] starting video stream...") vs = VideoStream(src=0).start() # vs = VideoStream(usePiCamera=True).start() time.sleep(2.0) # start the FPS counter fps = FPS().start() counter_i = 0 # loop over frames from the video file stream previous_data = None while True: # grab the frame from the threaded video stream and resize it # to 500px (to speedup processing) frame = vs.read() frame = imutils.resize(frame, width=500) # convert the input frame from (1) BGR to grayscale (for face # detection) and (2) from BGR to RGB (for face recognition) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) im = gray decodedObjects = decode(im, log=True) frame = markBarcode(frame, decodedObjects) data = actual_data(decodedObjects) # draw the predicted face name on the image #cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) #y = top - 15 if top - 15 > 15 else top + 15 #cv2.putText(frame, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2) # display the image to our screen cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == 27: break # update the FPS counter fps.update() #barcode detection program if data: if previous_data == data: if counter_i == 1: start_time = time.time() counter_i += 1 else: counter_i = 0 previous_data = data if counter_i > detection_threshold: detection_time = time.time() - start_time break # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop() message = data return message, detection_time
def eyeBlinkStartThFixed(self): # inizializzazione di counter e total, counter conta i frame consecutivi in EAR < threshold, total il numero di # eyeblink COUNTER = 0 TOTAL = 0 # dichiarazione e inizializzazione dei threshold che vanno da 0.10 a 0.29 ear_th = [] for threshold in np.arange(0.10, 0.30, 0.01): ear_th.append(0) print(self.inputType) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # inputType ha il suo interno il path + il video # poi inizia il video stream. fvs = FileVideoStream(self.inputType).start() time.sleep(1.0) # avvia il timer fps fps = FPS().start() num_frames = 0 # si cicla sui frame presi dal video stream per un massimo di 150 frame, visto che è sufficiente per capire # se si è verificato o meno un eyeblink. while fvs.more() and num_frames < 150: frame = fvs.read() try: frame = imutils.resize(frame, width=300) except Exception as e: print(str(e)) frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # frame = np.dstack([frame, frame, frame]) # viene richiamata la funzione che va ad analizzare ogni singolo frame per vedere se ci è stato # un eyeblink o meno try: eyesdetect, COUNTER, ear_th = self.eye_blink_video_fixedTh(frame, detector, predictor, COUNTER, ear_th) except Exception as e: print(str(e)) continue # viene aggiornato fps e incrementato il numero di frame. fps.update() fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) num_frames += 1 cv2.destroyAllWindows() fvs.stop() # ritorna la lista dei valori relativi ai threshold return ear_th
def recog(data): fa = cv2.CascadeClassifier('faces.xml') process = 0 cap = cv2.VideoCapture(0) fps = FPS().start() while True: ret, frame = cap.read() # for testing on sample data, remove above two lines and use belwo three lines #faces = os.listdir("faces") # for i in faces: # frame=cv2.imread('faces/'+i) frame = cv2.flip(frame, 1) frame = imutils.resize(frame, width=500) if (process % 15 is 0): #face_locations = face_recognition.face_locations(rgb_frame) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) rects = fa.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=6, minSize=(30, 30)) face_locations = [(y, x + w, y + h, x) for (x, y, w, h) in rects] face_encodings = face_recognition.face_encodings( rgb_frame, face_locations) for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings): #matches = face_recognition.compare_faces(data["encodings"], face_encoding,tolerance=0.55) name = "No Match" mean = {name: 0} face_distances = face_recognition.face_distance( data["encodings"], face_encoding) matches = [(i < 0.52) for i in face_distances] if True in matches: matchedIdxs = [i for (i, b) in enumerate(matches) if b] counts = {} mean = {} for i in matchedIdxs: name = data["names"][i] counts[name] = counts.get(name, 0) + 1 mean[name] = (mean.get(name, 0) + face_distances[i]) for i in mean: mean[i] /= counts[i] name = min(mean, key=mean.get) cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 1) cv2.rectangle(frame, (left, bottom + 20), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, '{:.2f}'.format(mean[name]), (left + 6, top + 15), font, 0.5, (255, 255, 255), 1) cv2.putText(frame, name, (left + 5, bottom + 15), font, 0.5, (255, 255, 255), 1) process += 1 cv2.imshow('Video', frame) if cv2.waitKey(10) & 0xFF == ord('q'): break fps.update() fps.stop() print("Elasped time: {:.2f}".format(fps.elapsed())) print("Approx. FPS: {:.2f}".format(fps.fps())) cap.release() cv2.destroyAllWindows() return mean, face_distances
def eyeBlinkStart(self): # inizializzazione di counter e total, counter conta i frame consecutivi in EAR < threshold, total il numero di # eyeblink COUNTER = 0 TOTAL = 0 # se inputType contiene un path per un video allora... if self.inputType is not None: ear_top = 0 history = ' ' print(self.inputType) detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # avvia il thread del video stream fvs = FileVideoStream(self.inputType).start() time.sleep(1.0) # avvia il timer fps fps = FPS().start() # fileStream = True num_frames = 0 # si cicla sui frame presi dal video stream per un massimo di 150 frame, visto che è sufficiente per capire # se si è verificato o meno un eyeblink. while fvs.more() and num_frames < 150: frame = fvs.read() # ridimensionamento del frame a 300 di width try: frame = imutils.resize(frame, width=300) except Exception as e: print(str(e)) # il frame viene convertito in scala di grigi frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # frame = np.dstack([frame, frame, frame]) # viene richiamata la funzione che va ad analizzare ogni singolo frame per vedere se ci è stato # un eyeblink o meno try: eyesdetect, COUNTER, TOTAL, ear_top = self.eye_blink_video(frame, detector, predictor, COUNTER, TOTAL, ear_top) except Exception as e: print(str(e)) continue # history è una cronologia degli stati dell'occhio che vengono presi da ogni frame, può essere usata per # vedere l'andamento frame-by-frame. history += eyesdetect print(history) # se si verifica un eyeblink ritorniamo True if TOTAL > 0: cv2.destroyAllWindows() fvs.stop() return True # viene aggiornato fps e incrementato il numero di frame. fps.update() fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) num_frames += 1 # usciti dal while se non si è verificato Eyeblink ritorniamo False cv2.destroyAllWindows() fvs.stop() if TOTAL == 0: return False # se inputType è vuoto significa che usiamo la webcam elif self.inputType is None: # acquisiamo da webcam cap = cv2.VideoCapture(0) history = '' detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") ear_top = 0 while True: ret, frame = cap.read() # viene richiamata la funzione che va ad analizzare ogni singolo per vedere se ci è stato # un eyeblink o meno eyedetect, TOTAL, COUNTER, ear_top = self.eye_blink_cam(self, frame, ret, detector, predictor, COUNTER, TOTAL, ear_top) # history è una cronologia degli stati dell'occhio che vengono presi da ogni frame, può essere usata per # vedere l'andamento frame-by-frame. history += eyedetect # se total è maggiore di 0 significa che un eyeblink è avvenuto e ritorniamo true if TOTAL >= 1: cv2.putText(frame, "Real", (0, 450), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3, cv2.LINE_AA) cap.release() cv2.destroyAllWindows() print("EYEBLINK: REAL") return True # se ciò non avviene per 200 frame ritorniamo false elif len(history) > 200: print(history) result = self.isBlinking(history, 3) print(result) cv2.putText(frame, "Fake", (0, 450), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 3, cv2.LINE_AA) cap.release() cv2.destroyAllWindows() print("EYEBLINK: FAKE") return False else: cv2.putText(frame, "Checking...", (0, 450), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 165, 255), 3, cv2.LINE_AA) cv2.imshow("Frame", frame) cv2.waitKey(1) # if the `q` key was pressed, break from the loop cap.release() cv2.destroyAllWindows()
# Grab the frame from the stream and resize it to have a max # width of 400 pixels (grabbed, frame) = stream.read() # frame = imutils.resize(frame, width=400) # Check to see if the frame should be displayed to our stream if args["display"] > 0: cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # Update the FPS counter fps.update() # Stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx FPS: {:.2f}".format(fps.fps())) # Cleanup stream.release() cv2.destroyAllWindows() # Create a threaded video stream, allow the camera sensor to warm up, # and start the FPS counter print("[INFO] sampling THREADED frames from the webcam...") vs = WebcamVideoStream(src=0).start() fps = FPS().start() # Loop over some frames, this time using the threaded stream while fps._numFrames < args["num_frames"]:
def text_video(): # initialize the original frame dimensions, new frame dimensions, # and ratio between the dimensions (W, H) = (None, None) (newW, newH) = (width, height) (rW, rH) = (None, None) # define the two output layer names for the EAST detector model that # we are interested -- the first is the output probabilities and the # second can be used to derive the bounding box coordinates of text layerNames = ["feature_fusion/Conv_7/Sigmoid", "feature_fusion/concat_3"] # load the pre-trained EAST text detector print("[INFO] loading EAST text detector...") net = cv2.dnn.readNet(east) # if a video path was not supplied, grab the reference to the web cam print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(1.0) # start the FPS throughput estimator fps = FPS().start() # loop over frames from the video stream while True: # grab the current frame, then handle if we are using a # VideoStream or VideoCapture object frame = vs.read() # check to see if we have reached the end of the stream if frame is None: break # resize the frame, maintaining the aspect ratio frame = imutils.resize(frame, width=1000) orig = frame.copy() # if our frame dimensions are None, we still need to compute the # ratio of old frame dimensions to new frame dimensions if W is None or H is None: (H, W) = frame.shape[:2] rW = W / float(newW) rH = H / float(newH) # resize the frame, this time ignoring aspect ratio frame = cv2.resize(frame, (newW, newH)) # construct a blob from the frame and then perform a forward pass # of the model to obtain the two output layer sets blob = cv2.dnn.blobFromImage(frame, 1.0, (newW, newH), (123.68, 116.78, 103.94), swapRB=True, crop=False) net.setInput(blob) (scores, geometry) = net.forward(layerNames) # decode the predictions, then apply non-maxima suppression to # suppress weak, overlapping bounding boxes (rects, confidences) = decode_predictions(scores, geometry) boxes = non_max_suppression(np.array(rects), probs=confidences) # loop over the bounding boxes for (startX, startY, endX, endY) in boxes: # scale the bounding box coordinates based on the respective # ratios startX = int(startX * rW) startY = int(startY * rH) endX = int(endX * rW) endY = int(endY * rH) # draw the bounding box on the frame cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 255, 0), 2) im_c = orig[startY:endY, startX:endX] im_c = cv2.cvtColor(im_c, cv2.COLOR_BGR2GRAY) im_c = cv2.threshold(im_c, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] cv2.imwrite('test.png', im_c) im_text = pytesseract.image_to_string('test.png', config=config) cv2.putText(orig, im_text, (startX - 10, startY - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2) print(im_text) os.remove('test.png') cv2.imshow("Text Detected", im_c) # update the FPS counter fps.update() # show the output frame cv2.imshow("Text Detection", orig) key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q"): break # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # if we are using a webcam, release the pointer vs.stop() # close all windows cv2.destroyAllWindows()
def main(): camera = VideoStream(src=0).start() # camera = VideoStream(usePiCamera=True).start() time.sleep(2.0) fps = FPS().start() count_frame = 1 # Define predictor model net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"]) predictor = dlib.shape_predictor(args["landmarks_predictor"]) # Change width and height of aligned face if necessary, default is 160x160 faceAlignment = FaceAligner(predictor, desiredFaceWidth = 160, desiredFaceHeight = 160) confidence = args["confidence"] # connect to server on local computer # local host IP '127.0.0.1' host = '223.195.37.238' # Define the port on which you want to connect port = 12345 while True: print("Frame ", count_frame) data_send = ImageData() aligned_face_bytestream_temp = [] box_posx_temp = [] box_posy_temp = [] box_w_temp = [] box_h_temp = [] frame = camera.read() frame = imutils.resize(frame, width = 800) cv2.imshow("Frame", frame) fps.update() key = cv2.waitKey(1) if key & 0xFF == ord("q"): break # For capturing an image, press button c elif key & 0xFF == ord("c"): # Do the face detection forward blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), False, False) net.setInput(blob) detections = net.forward() box_posx, box_posy, box_w, box_h, aligned_faces = detect_faces(frame, detections, faceAlignment, confidence) print("Send an image at frame", count_frame) # Detect faces and their information box_posx_temp.extend(box_posx) box_posy_temp.extend(box_posy) box_w_temp.extend(box_w) box_h_temp.extend(box_h) aligned_face_bytestream_temp.extend(aligned_faces) # Convert into bytestream data_send.img_bytestream = convert_img_to_bytestream(frame) data_send.aligned_face_bytestream = pickle.dumps(aligned_face_bytestream_temp) data_send.box_posx = pickle.dumps(box_posx_temp) data_send.box_posy = pickle.dumps(box_posy_temp) data_send.box_w = pickle.dumps(box_w_temp) data_send.box_h = pickle.dumps(box_h_temp) data_string = pickle.dumps(data_send) # Send the information to server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) s.sendall(data_string) s.close() count_frame = count_frame + 1 fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() camera.stop()
def main(): # TensorFlow graph = load_graph(args.frozen_model_file) image_tensor = graph.get_tensor_by_name('image_tensor:0') output_tensor = graph.get_tensor_by_name('generate_output/output:0') config = tf.ConfigProto() config.gpu_options.allow_growth=True sess = tf.Session(graph=graph, config=config) if args.video is not None: cap = WebcamVideoStream(args.video).start() else: cap = WebcamVideoStream(args.video_source).start() if args.video_out is not None: fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(args.video_out, fourcc, args.fps, (int(512*args.scale),int(256*args.scale))) fps = FPS().start() landmark_toggle = False last_image = np.zeros((256,256), dtype=np.uint8) count = 0 while(True): ret, frame = cap.read() if ret is True: if args.skip and count % args.skip != 0: print("Skipping",count) continue else: if args.zoom > 1: o_h, o_w, _ = frame.shape frame = cv2.resize(frame, None, fx=args.zoom, fy=args.zoom) h, w, _ = frame.shape off_h, off_w = int((h - o_h) / 2), int((w - o_w) / 2) frame = frame[off_h:h-off_h, off_w:w-off_w, :] if args.downsample > 1: downsample_scale = args.downsample else: # Auto-scale to CROP_SIZE small_side = min(frame.shape[0], frame.shape[1]) downsample_scale = 1 / (CROP_SIZE / small_side) # resize image and detect face frame_resize = cv2.resize(frame, None, fx=1 / downsample_scale, fy=1 / downsample_scale) count += 1 print("Frame:",count,"Shape:",frame_resize.shape) # print(frame_resize.shape) # print (frame_resize.shape) gray = cv2.cvtColor(frame_resize, cv2.COLOR_BGR2GRAY) faces = detector(gray, 1) black_image = np.zeros(frame_resize.shape, np.uint8) # black_image = np.zeros(frame.shape, np.uint8) for face in faces: detected_landmarks = predictor(gray, face).parts() landmarks = [[p.x, p.y] for p in detected_landmarks] color = (255, 255, 255) thickness = 3 if args.points: jaw = landmarks[0:17] left_eyebrow = landmarks[22:27] right_eyebrow = landmarks[17:22] nose_bridge = landmarks[27:31] lower_nose = landmarks[30:35] left_eye = landmarks[42:48] right_eye = landmarks[36:42] outer_lip = landmarks[48:60] inner_lip = landmarks[60:68] for part in [jaw, left_eyebrow, right_eyebrow, nose_bridge, lower_nose, left_eye, right_eye, outer_lip, inner_lip]: for x,y in part: cv2.circle(black_image, (x, y), 1, (255, 255, 255), -1) else: jaw = reshape_for_polyline(landmarks[0:17]) left_eyebrow = reshape_for_polyline(landmarks[22:27]) right_eyebrow = reshape_for_polyline(landmarks[17:22]) nose_bridge = reshape_for_polyline(landmarks[27:31]) lower_nose = reshape_for_polyline(landmarks[30:35]) left_eye = reshape_for_polyline(landmarks[42:48]) right_eye = reshape_for_polyline(landmarks[36:42]) outer_lip = reshape_for_polyline(landmarks[48:60]) inner_lip = reshape_for_polyline(landmarks[60:68]) cv2.polylines(black_image, [jaw], False, color, thickness) cv2.polylines(black_image, [left_eyebrow], False, color, thickness) cv2.polylines(black_image, [right_eyebrow], False, color, thickness) cv2.polylines(black_image, [nose_bridge], False, color, thickness) cv2.polylines(black_image, [lower_nose], True, color, thickness) cv2.polylines(black_image, [left_eye], True, color, thickness) cv2.polylines(black_image, [right_eye], True, color, thickness) cv2.polylines(black_image, [outer_lip], True, color, thickness) cv2.polylines(black_image, [inner_lip], True, color, thickness) # generate prediction if len(faces) > 0: combined_image = np.concatenate([resize(black_image), resize(frame_resize)], axis=1) image_rgb = cv2.cvtColor(combined_image, cv2.COLOR_BGR2RGB) # OpenCV uses BGR instead of RGB generated_image = sess.run(output_tensor, feed_dict={image_tensor: image_rgb}) image_bgr = cv2.cvtColor(np.squeeze(generated_image), cv2.COLOR_RGB2BGR) image_normal = np.concatenate([resize(frame_resize), image_bgr], axis=1) image_landmark = np.concatenate([resize(black_image), image_bgr], axis=1) if landmark_toggle or args.display_landmark == 1: img = image_landmark else: img = image_normal elif args.skip_fails: # Don't show/write frames where no face is detected continue else: img = last_image last_image = img if args.scale != 1: img = cv2.resize(img, None, fx=args.scale, fy=args.scale) if args.video_out is not None: out.write(img) if args.no_gui is False: cv2.imshow('face2face', img) fps.update() key = cv2.waitKey(10) if key & 0xFF == ord('q'): break elif key & 0xFF == ord('m'): landmark_toggle = not landmark_toggle else: # We're done here break fps.stop() print('[INFO] elapsed time (total): {:.2f}'.format(fps.elapsed())) print('[INFO] approx. FPS: {:.2f}'.format(fps.fps())) sess.close() cap.stop() if args.video_out is not None: out.release() cv2.destroyAllWindows()
def recognize(): detector = cv2.dnn.readNetFromCaffe(DEPLOY_PROTOTXT_PATH, RES10_300X300_PATH) #print("[INFO] loading face recognizer...") #embedder = cv2.dnn.readNetFromTorch(args["embedding_model"]) recognizer = pickle.loads(open(RECOGNIZER_PATH, "rb").read()) le = pickle.loads(open(LABEL_ENCODER_PATH, "rb").read()) print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) fps = FPS().start() tstart = time.time() while True: frame = vs.read() frame = imutils.resize(frame, width=600) (h, w) = frame.shape[:2] imageBlob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0), swapRB=False, crop=False) detector.setInput(imageBlob) detections = detector.forward() for i in range(0, detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > CONFIDENCE: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") face = frame[startY:endY, startX:endX] (fH, fW) = face.shape[:2] if fW < 20 or fH < 20: continue #faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, # (96, 96), (0, 0, 0), swapRB=True, crop=False) #embedder.setInput(faceBlob) #vec = embedder.forward() coor = [((startY, startX + (endX - startX), startY + (endY - startY), startX))] rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) encodings = face_recognition.face_encodings(rgb_frame, coor) preds = recognizer.predict_proba(encodings)[0] j = np.argmax(preds) proba = preds[j] name = le.classes_[j] text = "{}: {:.2f}%".format(name, proba * 100) y = startY - 10 if startY - 10 > 10 else startY + 10 cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2) cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) fps.update() cv2.imshow("Frame", frame) # tcur = time.time() # if tcur - tstart > 15 & name != 'unknown' & proba > 0.8: # return name # elif tcur - tstart > 60: # return 'unknown' key = cv2.waitKey(1) & 0xFF if key == ord("q"): break fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) cv2.destroyAllWindows() vs.stop()
print("[WARNING] Survvi has detected motion from webcam... "); disp_state = 1; # start displaying frame # check to see if motion should be displayed to our screen if (args["display"] > 0) and (disp_state == 1): cv2.imshow("Camera Feed", frame) cv2.imshow("Survvi Feed", gray_frame); key = cv2.waitKey(1) & 0xFF elif (disp_state == 0): # clear screen now that we no longer show images print("[INFO] Survvi detects no motion from webcam... "); cv2.destroyAllWindows(); disp_state = 2; elif (disp_state == 2): pass; key = cv2.waitKey(1) & 0xFF; # bg_frame = gray_frame; # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] Survvi up-time: {:.2f}".format(fps.elapsed())) print("[INFO] Survvi approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop() # camera.stop();
def objectdetection(): # load model prototxt = 'mobilenet_ssd/MobileNetSSD_deploy.prototxt' model = 'mobilenet_ssd/MobileNetSSD_deploy.caffemodel' #label variable #will change it to user input labels = "cat" #confidence level of model confidence = 0.7 #video input either stream or video file video = 0 #video= 'input/cat.mp4' # 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) # initialize the video stream, dlib correlation tracker, output video # writer, and predicted class label print("[INFO] starting video stream...") vs = cv2.VideoCapture(video) tracker = None writer = None label = "" # start the frames per second throughput estimator fps = FPS().start() # loop over frames from the video file stream while True: # grab the next frame from the video file (grabbed, frame) = vs.read() # check to see if we have reached the end of the video file if frame is None: break # resize the frame for faster processing and then convert the # frame from BGR to RGB ordering (dlib needs RGB ordering) frame = imutils.resize(frame, width=600) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # if our correlation object tracker is None we first need to # apply an object detector to seed the tracker with something # to actually track if tracker is None: # grab the frame dimensions and convert the frame to a blob (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(frame, 0.007843, (w, h), 127.5) # pass the blob through the network and obtain the detections # and predictions net.setInput(blob) detections = net.forward() # ensure at least one detection is made if len(detections) > 0: # find the index of the detection with the largest # probability -- out of convenience we are only going # to track the first object we find with the largest # probability; future examples will demonstrate how to # detect and extract *specific* objects i = np.argmax(detections[0, 0, :, 2]) # grab the probability associated with the object along # with its class label conf = detections[0, 0, i, 2] label = CLASSES[int(detections[0, 0, i, 1])] # filter out weak detections by requiring a minimum # confidence if conf > confidence and label == labels: # 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) # draw the bounding box and text for the object cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), 2) cv2.putText(frame, label, (startX, startY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 2) # otherwise, we've already performed detection so let's track # the object else: # update the tracker and grab the position of the tracked # object 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()) # draw the bounding box from the correlation object tracker cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0), 2) cv2.putText(frame, label, (startX, startY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 255, 0), 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 # update the FPS counter fps.update() # stop the timer and display FPS information fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # check to see if we need to release the video writer pointer if writer is not None: writer.release() # do a bit of cleanup cv2.destroyAllWindows() vs.release()
if (prevNames.__contains__(n) == False): logins.append(n) for n in prevNames: if (names.__contains__(n) == False): logouts.append(n) # send inforrmation to prompt, only if something has changes if (logins.__len__() > 0): printjson("login", {"names": logins}) if (logouts.__len__() > 0): printjson("logout", {"names": logouts}) # set this names as new prev names for next iteration prevNames = names key = cv2.waitKey(1) & 0xFF # if the `q` key was pressed, break from the loop if key == ord("q") or closeSafe == True: break time.sleep(args["interval"] / 1000) # stop the timer and display FPS information fps.stop() printjson("status", "elasped time: {:.2f}".format(fps.elapsed())) printjson("status", "approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() vs.stop()
fps = FPS().start() # This will run while there is a new frame in the video file or # while the user do not press the "q" (quit) keyboard button. while True: # Capture the frame retval, frame = capture.read() # Update the FPS counter. fps.update() # Check if there is a valid frame. if not retval: break # Display the resulting frame. cv2.imshow("Video", frame) key = cv2.waitKey(1) if key & 0xFF == ord("q"): break # Stop the timer and display FPS information. fps.stop() print("Elasped time: {:.2f}".format(fps.elapsed())) print("Camera framerate: {:.2f}".format(fps.fps())) # When everything done, release the capture object. capture.release() cv2.destroyAllWindows()
def main(): width = 800 height = 600 # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--shape-predictor", required=True, help="path to facial landmark predictor") ap.add_argument("-v", "--video", required=True, help="path to input video") #ap.add_argument("-n", "--num-frames", type=int, default=2000, # help="# of frames to loop over for FPS test") args = vars(ap.parse_args()) #initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(args["shape_predictor"]) clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) # load the input image, resize it, and convert it to grayscale #cap = cv2.VideoCapture(args["video"]) print("[INFO] starting video file thread...") cap = cv2.VideoCapture(args["video"]) #time.sleep(1) # start the FPS timer fps = FPS().start() window_name = "cctv" cv2.namedWindow(window_name, WINDOW_NORMAL) cv2.resizeWindow(window_name, width, height) if cap.isOpened(): ret, image = cap.read() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Assign new height and width for ROI (xr, yr, wr, hr) = cv2.boundingRect(gray) startYROI = 3 * hr / 5 endYROI = hr - 1 * hr / 20 else: print "Camera is not open" while ret: cv2.rectangle(image, (0, startYROI), (wr, endYROI), (0, 255, 0), 2) roi_image = image[startYROI:endYROI, 0:wr] #image = cv2.imread(args["image"]) #image = cv2.resize(image, (width, height), interpolation = cv2.INTER_CUBIC) #image_scaled = cv2.resize(image, (600, 500), interpolation=cv2.INTER_AREA) gray = cv2.cvtColor(roi_image, cv2.COLOR_BGR2GRAY) #pil_gray = Image.fromarray(gray) #pil_gray = pil_gray.resize((width, height), Image.BICUBIC) #cv_gray = np.array(pil_gray) #roi = image[] # detect faces in the grayscale image rects = detector(gray, 0) # loop over the face detections 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 = 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(image, (x, (y + startYROI)), (x + w, (y + startYROI) + h), (0, 255, 0), 2) # show the face number cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y + startYROI - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (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(image, (x, (y + startYROI)), 1, (0, 0, 255), -1) # show the output image with the face detections + facial landmarks cv2.imshow(window_name, image) ret, image = cap.read() if cv2.waitKey(1) & 0xff == ord('q'): break fps.update() fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cap.release() cv2.destroyAllWindows()
def run(self): self.signals.changeTitleBox.emit(" Sol Toplam\n" "Sağ Toplam\n" " Durum") self.vs = cv2.VideoCapture(self.video_source) detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(self.model_path, 'rb') as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') label_map = label_map_util.load_labelmap(self.label_path) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=self.num_classes, use_display_name=True) category_index = label_map_util.create_category_index(categories) W = None H = None ct = CentroidTracker(maxDisappeared=40, maxDistance=50) trackers = [] trackableObjects = {} totalFrames = 0 skip_frame = 10 fps = FPS().start() # Operation with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: while True: ret, self.frame = self.vs.read() if self.frame is None or self.stopped: print("Video stream ended.") break self.frame = imutils.resize( self.frame, width=1000) # Less data we have, faster we are. rgb = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB) self.frame = rgb if W is None or H is None: (H, W, ch) = self.frame.shape self.status = "Bekliyor" rects = [] if totalFrames % skip_frame == 0: self.status = "Saptanıyor" trackers = [] frame_expanded = np.expand_dims(self.frame, axis=0) image_tensor = detection_graph.get_tensor_by_name( 'image_tensor:0') boxes = detection_graph.get_tensor_by_name( 'detection_boxes:0') scores = detection_graph.get_tensor_by_name( 'detection_scores:0') classes = detection_graph.get_tensor_by_name( 'detection_classes:0') num_detections = detection_graph.get_tensor_by_name( 'num_detections:0') (boxes, scores, classes, num_detections) = sess.run( [boxes, scores, classes, num_detections], feed_dict={image_tensor: frame_expanded}) ymin = int((boxes[0][0][0] * H)) xmin = int((boxes[0][0][1] * W)) ymax = int((boxes[0][0][2] * H)) xmax = int((boxes[0][0][3] * W)) box_area = (xmax - xmin) * (ymax - ymin) total_area = W * H # For eliminating the false positives. if box_area > total_area * 0.5: ymin, xmin, xmax, ymax = (None, None, None, None) if ymin is not None: tracker = dlib.correlation_tracker() rect = dlib.rectangle(xmin, ymin, xmax, ymax) tracker.start_track(rgb, rect) trackers.append(tracker) else: for tracker in trackers: self.status = "Takip Ediliyor" tracker.update(rgb) pos = tracker.get_position() xmin = int(pos.left()) ymin = int(pos.top()) xmax = int(pos.right()) ymax = int(pos.bottom()) rects.append((xmin, ymin, xmax, ymax)) # cv2.line(self.frame, (W // 2, 0), (W // 2, H), (0, 255, 255), 2) objects = ct.update(rects) for (objectID, centroid) in objects.items(): trackable_obj = trackableObjects.get(objectID, None) if trackable_obj is None: trackable_obj = TrackableObject(objectID, centroid) else: x = [c[0] for c in trackable_obj.centroids] direction = centroid[0] - np.mean(x) trackable_obj.centroids.append(centroid) if not trackable_obj.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[0] < int( W * 0.25): self.totalLeft += 1 trackable_obj.counted = True elif direction > 0 and centroid[0] > int( W * 0.75): self.totalRight += 1 trackable_obj.counted = True trackableObjects[objectID] = trackable_obj text = "ID {}".format(objectID) cv2.putText(self.frame, text, (centroid[0] - 10, centroid[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) cv2.circle(self.frame, (centroid[0], centroid[1]), 4, (0, 255, 0), -1) self.signals.changeTextBox.emit( f"{self.totalLeft}\n{self.totalRight}\n{self.status}") # End of the loop bytesPerLine = ch * W convertToQtFormat = QImage(rgb.data, W, H, bytesPerLine, QImage.Format_RGB888) p = convertToQtFormat.scaled(800, 600, Qt.KeepAspectRatio) self.signals.changePixmap.emit(p) totalFrames += 1 fps.update() # self.signals.changeTitleBox.emit("Durum: ") # Clear output self.signals.changeTextBox.emit("Rapor kaydedildi.") # Alter button to Start. self.signals.changeButton.emit("start_button") # Stop FPS count. fps.stop() # Get total elapsed time. self.total_elapsed_time = fps.elapsed() # Create report to database. self.create_report(self.totalLeft, self.totalRight, fps.elapsed()) # Finally, set placeholder. self.signals.changePixmap.emit(QImage('./Resources/placeholder2.png'))
def evaluateallframes(base_dir, filename, outputfile="./output_video1.mp4"): Final_array = np.load(base_dir / filename, allow_pickle=True, mmap_mode='r') data_array = Final_array['arr_0'] array_len = len(data_array) # 20, 200 frames in one file, downsample by 10 print("Final_array lenth:", array_len) print("Final_array type:", type(data_array)) # numpy.ndarray frame_width = 1920 frame_height = 1280 out = cv2.VideoWriter(outputfile, cv2.VideoWriter_fourcc('M', 'P', '4', 'V'), 2, (frame_width, frame_height)) fps = FPS().start() wod_latency_submission.initialize_model() required_field = wod_latency_submission.DATA_FIELDS print(required_field) for frameid in range(array_len): #frameid = 5 print("frameid:", frameid) # {'key':key, 'context_name':context_name, 'framedict':framedict} convertedframesdict = data_array[frameid] frame_timestamp_micros = convertedframesdict['key'] context_name = convertedframesdict['context_name'] framedict = convertedframesdict['framedict'] # 10017090168044687777_6380_000_6400_000 #print('context_name:', context_name) #print('frame_timestamp_micros:', frame_timestamp_micros) # 1550083467346370 #result = wod_latency_submission.run_model(framedict[required_field[0]], framedict[required_field[1]]) #result = wod_latency_submission.run_model(**framedict) Front_image = framedict[required_field[0]] start_time = time.time() result = wod_latency_submission.run_model(Front_image) end_time = time.time() elapsed_time = end_time - start_time print('Inference time: ' + str(elapsed_time) + 's') #print(result) #Save the original image #output_path = "./test.png" #visualization_util.save_image_array_as_png(Front_image, output_path) image_np_with_detections = Front_image.copy() visualization_util.visualize_boxes_and_labels_on_image_array( image_np_with_detections, result['boxes'], result['classes'], result['scores'], category_index, use_normalized_coordinates=False, max_boxes_to_draw=200, min_score_thresh=Threshold, agnostic_mode=False) #visualization_util.save_image_array_as_png(image_np_with_detections, outputfile) name = './Test_data/frame' + str(frameid) + '.jpg' #print ('Creating\...' + name) #cv2.imwrite(name, image_np_with_detections) #write to image folder fps.update() out.write(image_np_with_detections) # 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())) out.release()
if args["display"] > 0: cv2.imshow("Frame", frame) key = cv2.waitKey(1) & 0xFF # clear the stream in preparation for the next frame and update # the FPS counter rawCapture.truncate(0) fps.update() # check to see if the desired number of frames have been reached if i == args["num_frames"]: break # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() stream.close() rawCapture.close() camera.close() # created a *threaded *video stream, allow the camera sensor to warmup, # and start the FPS counter print("[INFO] sampling THREADED frames from `picamera` module...") vs = PiVideoStream().start() time.sleep(2.0) fps = FPS().start()
window_name = 'preview' # Creation du thread de lecture + setup vs=PiVideoStream() vs.camera.video_stabilization = True # Demarrage du flux video + warmup de la camera vs.start() time.sleep(2.0) # Creation de la fenetre d'affichage cv2.namedWindow(window_name, cv2.WINDOW_AUTOSIZE) fps = FPS().start() while True : frame = vs.read() fps.update() cv2.imshow(window_name, frame) key = cv2.waitKey(1) & 0xFF if key == ord("q") : break fps.stop() print("Temps passé : {:.2f}".format(fps.elapsed())) print("Approx. FPS : {:.2f}".format(fps.fps())) cv2.destroyAllWindows() vs.stop()
def find_vehicles(INPUT_FILE, OUTPUT_FILE, tiny): if tiny: CONFIG_FILE = 'yolov3-tiny.cfg' WEIGHTS_FILE = 'yolov2-tiny.weights' else: CONFIG_FILE = 'yolov3.cfg' WEIGHTS_FILE = 'yolov3.weights' H = None W = None fps = FPS().start() fourcc = cv2.VideoWriter_fourcc(*"MJPG") writer = cv2.VideoWriter(OUTPUT_FILE, fourcc, 30, (800, 600), True) LABELS = open(LABELS_FILE).read().strip().split("\n") np.random.seed(4) COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8") net = cv2.dnn.readNetFromDarknet(CONFIG_FILE, WEIGHTS_FILE) vs = cv2.VideoCapture(INPUT_FILE) # determine only the *output* layer names that we need from YOLO ln = net.getLayerNames() ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] cnt = 0 while True: cnt += 1 print("Frame number", cnt) try: (grabbed, image) = vs.read() except: break if image is None: break blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) if W is None or H is None: (H, W) = image.shape[:2] layerOutputs = net.forward(ln) # initialize our lists of detected bounding boxes, confidences, and # class IDs, respectively boxes = [] confidences = [] classIDs = [] # loop over each of the layer outputs for output in layerOutputs: # loop over each of the detections for detection in output: # extract the class ID and confidence (i.e., probability) of # the current object detection scores = detection[5:] classID = np.argmax(scores) confidence = scores[classID] # filter out weak predictions by ensuring the detected # probability is greater than the minimum probability if confidence > CONFIDENCE_THRESHOLD: # scale the bounding box coordinates back relative to the # size of the image, keeping in mind that YOLO actually # returns the center (x, y)-coordinates of the bounding # box followed by the boxes' width and height box = detection[0:4] * np.array([W, H, W, H]) (centerX, centerY, width, height) = box.astype("int") # use the center (x, y)-coordinates to derive the top and # and left corner of the bounding box x = int(centerX - (width / 2)) y = int(centerY - (height / 2)) # update our list of bounding box coordinates, confidences, # and class IDs boxes.append([x, y, int(width), int(height)]) confidences.append(float(confidence)) classIDs.append(classID) # apply non-maxima suppression to suppress weak, overlapping bounding # boxes idxs = cv2.dnn.NMSBoxes(boxes, confidences, CONFIDENCE_THRESHOLD, CONFIDENCE_THRESHOLD) # ensure at least one detection exists if len(idxs) > 0: # loop over the indexes we are keeping for i in idxs.flatten(): # extract the bounding box coordinates (x, y) = (boxes[i][0], boxes[i][1]) (w, h) = (boxes[i][2], boxes[i][3]) color = [int(c) for c in COLORS[classIDs[i]]] cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i]) cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) # show the output image cv2.imshow("output", cv2.resize(image, (800, 600))) writer.write(cv2.resize(image, (800, 600))) fps.update() key = cv2.waitKey(1) & 0xFF if key == ord("q"): break 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() # release the file pointers print("[INFO] cleaning up...") writer.release() vs.release()
def calculate_fps(self, frames_no=100): fps = FPS().start() # Don't wanna display window if self.debug: self.debug = not self.debug for i in range(0, frames_no): self.where_lane_be() fps.update() fps.stop() # Don't wanna display window if not self.debug: self.debug = not self.debug print('Time taken: {:.2f}'.format(fps.elapsed())) print('~ FPS : {:.2f}'.format(fps.fps()))