Example #1
0
def cell_8():
    cap5 = VideoStream(source).start()

    if cap5.isOpened():
        cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
        while True:
            success, frame8 = cap5.read()
            image8 = cv2.imread('cover8.jpg')
            alpha = 0.4
            added_img8 = cv2.addWeighted(frame8, alpha, image8, 1 - alpha, 0)
            img8 = cv2.resize(added_img8, (0, 0), fx=0.69, fy=0.65)

            font = cv2.FONT_HERSHEY_SIMPLEX
            cv2.putText(img8, 'Cell 8', (350, 150), font, 5.0, (0, 0, 0), 5,
                        cv2.LINE_AA)

            cv2.imshow('demo', img8)
            if not success:
                break
            else:
                ret, buffer = cv2.imencode('.jpg', frame8)
                frame = buffer.tobytes()
                yield (b'--frame\r\n'
                       b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
            k = cv2.waitKey(10)
            if k & 0xFF == ord('q'):
                break

    else:
        print("camera open failed")

    cap5.release()
    cv2.destroyAllWindows()
Example #2
0
class VideoManager:
    def __init__(self, window_name, smile_threshold, is_micro_controller):
        self.window_name = window_name
        self.smile_threshold = smile_threshold
        self.is_micro_controller = is_micro_controller
        self.cap = None

    def start_video(self):
        if self.window_name is None:
            raise VideoError(f"No window name for video")
        cv2.namedWindow(self.window_name)
        if self.is_micro_controller == 1:
            self.cap = VideoStream(src=0).start()
            return True, self.cap
        else:
            self.cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
            return self.cap

    def stop_video(self):
        # cv2.destroyWindow(self.window_name)
        if self.is_micro_controller == 0:
            self.cap.release()
        else:
            self.cap.stream.release()

    def show_video(self, frame):
        cv2.imshow(self.window_name, frame)
        cv2.waitKey(1)

    def read_frame(self):
        if self.is_micro_controller == 0:
            if not self.cap.isOpened():
                raise VideoError(f"No video available to read")
                return False, False
        if self.is_micro_controller == 1:
            return True, self.cap.read()
        return self.cap.read()

    def put_text_on_frame(self, classes, frame, x, y):
        cv2.putText(frame, "Not Happy",
                    (x - 20, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                    (0, 255,
                     0), 2) if classes < self.smile_threshold else cv2.putText(
                         frame, "Happy",
                         (x - 20, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                         (0, 255, 0), 2)
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

# start the frames per second throughput estimator
fps = FPS().start()
(ret, frame) = vs.read()

# loop over frames from the video stream
while vs.isOpened():
    # grab the next frame and handle if we are reading from either
    # VideoCapture or VideoStream
    ret = False
    time.sleep(2.0)

    # 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
Example #4
0
def main():

    # initialize the bounding box coordinates of the object we are going to track
    detections = None
    counter = 0

    global _TAKEOFF
    global _LAND
    global _MOVE_DETECTION

    _TAKEOFF = True
    _LAND = False
    _MOVE_DETECTION = "HOLD"

    # Tell to drone to takeoff and wait few seconds
    write_detection(True, "TAKEOFF")
    time.sleep(10)

    if not args.get("video", False):
        video = VideoStream(src=0).start()
        time.sleep(1.0)
        fps = FPS().start()
    else:
        video = cv2.VideoCapture(args["video"])
        # Exit if video not opened
        if not video.isOpened():
            print("Could not open video")
            sys.exit()

        # Read first frame
        ok, frame = video.read()
        if not ok:
            print("Cannot read video file")
            sys.exit()

        frame = video.read()
        frame = frame[1] if args.get("video", False) else frame
        frame = cv2.resize(frame, (640, 480))

        # start the FPS throughput estimator as well
        fps = FPS().start()
        print(f"frame dimensions:{frame.shape}")

    print(f"Dimensions for output file:{frame_width}x{frame_height}")
    print("Starting loop...")
    # loop over frames from the video stream
    start_time = time.time()

    # start getting video stream and detect objects on frame by frame
    while True:
        counter += 1
        # grab the current frame, then handle if we are using a
        # VideoStream or VideoCapture object
        frame = video.read()
        # frame = cv2.flip(frame, 1)
        frame = frame[1] if args.get("video", False) else frame

        # check to see if we have reached the end of the stream
        if frame is None:
            break

        # resize the frame (so we can process it faster) and grab the
        # frame dimensions
        frame = cv2.resize(frame, (640, 480))
        (H, W) = frame.shape[:2]

        # if the input queue *is* empty, give the current frame to
        # classify
        if inputQueue.empty():
            inputQueue.put(frame)

        # if the output queue *is not* empty, grab the detections
        if not outputQueue.empty():
            detections = outputQueue.get()

        # check to see if our detectios are not None (and if so, we'll
        # draw the detections on the frame)
        if detections is not None:
            # loop over the detections
            for i in np.arange(0, detections.shape[2]):
                # extract the confidence (i.e., probability) associated
                # with the prediction
                confidence = detections[0, 0, i, 2]

                # filter out weak detections by ensuring the `confidence`
                # is greater than the minimum confidence
                if confidence > args["confidence"]:

                    # otherwise, extract the index of the class label from
                    # the `detections`, then compute the (x, y)-coordinates
                    # of the bounding box for the object
                    idx = int(detections[0, 0, i, 1])
                    dims = np.array([W, H, W, H])
                    box = detections[0, 0, i, 3:7] * dims
                    (startX, startY, endX, endY) = box.astype("int")

                    if idx == 1:  # 1 = Person:
                        bbox = (startX, startY, endX, endY)

                        # draw the prediction on the frame
                        label = "{}: {:.2f}%".format(CLASSES[idx],
                                                     confidence * 100)

                        # get rectangle cordinates
                        ax = bbox[0]
                        ay = bbox[1]

                        bx = bbox[2]
                        by = bbox[1]

                        cx = bbox[2]
                        cy = bbox[3]

                        dx = bbox[0]
                        dy = bbox[3]

                        x = [ax, bx, cx, dx]
                        y = [ay, by, cy, dy]

                        # calculate surface area of rectangle
                        w = math.sqrt((bx - ax) * (bx - ax) + (by - ay) *
                                      (by - ay))
                        h = math.sqrt((cx - bx) * (cx - bx) + (cy - by) *
                                      (cy - by))

                        surface = int(w * h)
                        print(f"SURFACE: {surface}")

                        # add current surface to queue (if we are using slower device we dont want to move for each frame)
                        avg_surfacesQueue.put(surface)

                        # calculate center of rectangle
                        circle_cord = ((startX + endX) // 2,
                                       (startY + endY) // 2)

                        # calculate direction of drone
                        get_direction(circle_cord)

                        avg_surfacesQueue.queue.clear()

                        print(f"Move: {_MOVE_DETECTION}")

                        cv2.rectangle(frame, (startX, startY), (endX, endY),
                                      COLORS[idx], 2)
                        cv2.circle(frame,
                                   circle_cord,
                                   radius=10,
                                   color=COLORS[idx],
                                   thickness=-1)

                        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)

                        print(f"Object detected: {label}")
                        print(f"Center X,Y: {circle_cord}")

                        # breaking detection loop to display only 1 object on frame (most confident one)
                        # if we don't do this we will sometimes get 2 objects which can disturb decision to which direction we are going.
                        break

        # update the FPS counter
        fps.update()
        fps.stop()
        # initialize the set of information we'll be displaying on
        # the frame
        info = [("FPS", "{:.2f}".format(fps.fps())), ("MOVE", _MOVE_DETECTION)]

        # loop over the info tuples and draw them on our frame
        for (i, (k, v)) in enumerate(info):
            text = "{}: {}".format(k, v)
            cv2.putText(frame, text, (10, H - ((i * 20) + 20)),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

        # horizontal and vertical line on frame, representing center of frame
        cv2.line(frame,
                 pt1=(frame_width // 2, 0),
                 pt2=(frame_width // 2, frame_height),
                 color=(0, 0, 255),
                 thickness=1)
        cv2.line(frame,
                 pt1=(0, frame_height // 2),
                 pt2=(frame_width, frame_height // 2),
                 color=(0, 0, 255),
                 thickness=1)

        # write calculated detection to file
        write_detection(False, _MOVE_DETECTION)
        out.write(frame)

        # if we have headless device we don't display frame
        if platform.uname()[4] == "x86_64":
            cv2.imshow("Frame", frame)

        key = cv2.waitKey(1) & 0xFF

        # if the `q` key was pressed or counter is reached, break from the loop
        if key == ord("q"):
            print("Key Q pressed...")
            break
        elif counter == 5000:
            break

    # last action when ending script is to land
    _TAKEOFF = False
    _LAND = True
    write_detection(True, "LAND")
    print("--- %s seconds ---" % (time.time() - start_time))

    # if we are using a webcam, release the pointer
    if not args.get("video", False):
        video.stop()
        out.release()

    # otherwise, release the file pointer
    else:
        video.release()
        out.release()

    # close all windows
    cv2.destroyAllWindows()
class HumanDetector(metaclass=Singleton):
    def __init__(self, find_humans_from_video_file_name=None,
                 use_pi_camera=USE_PI_CAMERA, open_display=OPEN_DISPLAY):
        # initialize the frame dimensions (we'll set them as soon as we read
        # the first frame from the video)
        self.H = None
        self.W = None
        self.video_stream = None
        self.net = None
        self.current_time_stamp = None
        self.frame = None
        self.rgb = None
        self.meter_per_pixel = None
        self.args = None
        parser = argparse.ArgumentParser()
        parser.add_argument("-i", "--peer_ip_address", type=str,
                            help="Provide the IP address of the remote raspberry PI.")
        parser.add_argument("-p", "--peer_port", type=int, help="Provide the server port of the remote raspberry PI.",
                            default=SERVER_PORT)
        parser.add_argument('-d', '--debug', type=bool, help='Enable debug logging.', default=False)
        self.args = parser.parse_args()
        if self.args.debug:
            Logger.set_log_level(logging.DEBUG)
        self.find_humans_from_video_file_name = find_humans_from_video_file_name
        self.use_pi_camera = use_pi_camera
        self.open_display = open_display
        self.perform_human_detection = True

        SendReceiveMessages().perform_job(peer_ip_address=self.args.peer_ip_address)

        # Load Model
        self.load_model()
        # Initialize the camera.
        self.initialize_camera()

        # start the frames per second throughput estimator
        self.fps = FPS().start()
        self.centroid_object_creator = CentroidObjectCreator()

    @classmethod
    def perform_job(cls):
        """
        This method performs the job expected out from this class.
        :return:
        """
        t1 = threading.Thread(target=HumanDetector().thread_for_face_tracker)
        t2 = threading.Thread(target=HumanDetector().get_and_print_total_face_count)

        # starting thread 1
        t1.start()
        t2.start()
        # return t1.join()

    @classmethod
    def perform_job_mac(cls):
        HumanDetector().thread_for_face_tracker()
        t1 = threading.Thread(target=HumanDetector().get_and_print_total_face_count)
        t1.start()

    def get_human_centroid_dict(self):
        """
        This is used for unit test purpose only.
        :return:
        """
        return HumanTrackerHandler.human_tracking_dict

    def load_model(self):
        """
        Load our serialized model from disk
        """
        Logger.logger().info("Loading model name:{}, proto_text:{}.".format(MODEL_NAME, PROTO_TEXT_FILE))
        self.net = cv2.dnn.readNetFromCaffe(os.path.join(
            os.path.dirname(os.path.realpath(__file__)),
            PROTO_TEXT_FILE),
            os.path.join(
                os.path.dirname(os.path.realpath(__file__)),
                MODEL_NAME))

        if self.use_pi_camera:
            # Set the target to the MOVIDIUS NCS stick connected via USB
            # Prerequisite: https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_raspbian.html
            Logger.logger().info("Setting MOVIDIUS NCS stick connected via USB as the target to run the model.")
            self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
        else:
            Logger.logger().info("Setting target to CPU.")
            self.net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)

    def initialize_camera(self):
        """
        Initialize the video stream and allow the camera sensor to warmup.
        """
        if self.find_humans_from_video_file_name:
            self.find_humans_from_video_file_name = \
                os.path.join(os.path.dirname(__file__),
                             self.find_humans_from_video_file_name)
            Logger.logger().info("Reading the input video file {}.".format(self.find_humans_from_video_file_name))

            self.video_stream = cv2.VideoCapture(self.find_humans_from_video_file_name)
            if not self.video_stream:
                Logger.logger().error("cv2.VideoCapture() returned None.")
                raise ValueError
            # self.video_stream.set(cv2.CAP_PROP_FPS, int(10))
        elif self.use_pi_camera:
            Logger.logger().info("Warming up Raspberry PI camera connected via the PCB slot.")
            self.video_stream = VideoStream(usePiCamera=True).start()
        else:
            Logger.logger().debug("Setting video capture device to {}.".format(VIDEO_DEV_ID))
            self.video_stream = VideoStream(src=VIDEO_DEV_ID).start()
        time.sleep(2.0)

    def grab_next_frame(self):
        """
        1. Grab the next frame from the stream.
        2. store the current timestamp, and store the new date.
        """
        if self.find_humans_from_video_file_name:
            if self.video_stream.isOpened():
                ret, self.frame = self.video_stream.read()
            else:
                Logger.logger().info("Unable to open video stream...")
                raise ValueError
        else:
            self.frame = self.video_stream.read()
        if self.frame is None:
            return

        self.current_time_stamp = datetime.now()
        # resize the frame
        self.frame = imutils.resize(self.frame, width=FRAME_WIDTH_IN_PIXELS)
        # width = FRAME_WIDTH_IN_PIXELS
        # height = self.frame.shape[0] # keep original height
        # dim = (width, height)
        # self.frame = cv2.resize(self.frame, dim, interpolation = cv2.INTER_AREA)
        self.rgb = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB)

    def set_frame_dimensions(self):
        """
        If the frame dimensions are empty, set them.
        """
        # if the frame dimensions are empty, set them
        if not self.W or not self.H:
            (self.H, self.W) = self.frame.shape[:2]

    def loop_over_streams(self):
        # while self.perform_human_detection:
        self.grab_next_frame()
        # check if the frame is None, if so, break out of the loop
        if self.frame is None:
            if self.find_humans_from_video_file_name:
                for _ in range(TIMEOUT_FOR_TRACKER + 1):
                    HumanTrackerHandler.compute_direction_for_dangling_object_ids(keep_dict_items=True)
                    time.sleep(1)
                self.perform_human_detection = False
                # break
            else:
                HumanTrackerHandler.compute_direction_for_dangling_object_ids()
                # continue
        self.set_frame_dimensions()

        objects = self.centroid_object_creator.create_centroid_tracker_object(self.H, self.W, self.rgb, self.net,
                                                                              self.frame)
        for speed_tracked_object, objectID, centroid in HumanTrackerHandler.yield_a_human_tracker_object(objects):
            HumanTrackerHandler.record_movement(speed_tracked_object)
            HumanValidator.validate_column_movement(speed_tracked_object, self.current_time_stamp, self.frame,
                                                    objectID)
        if self.find_humans_from_video_file_name:
            HumanTrackerHandler.compute_direction_for_dangling_object_ids(keep_dict_items=True)
        else:
            HumanTrackerHandler.compute_direction_for_dangling_object_ids()

        # if the *display* flag is set, then display the current frame
        # to the screen and record if a user presses a key
        if self.open_display:
            cv2.imshow("Human_detector_frame", self.frame)
            key = cv2.waitKey(1) & 0xFF

            # if the `q` key is pressed, break from the loop
            # if key == ord("q"):
            #     break

        # Update the FPS counter
        self.fps.update()

    def get_and_print_total_face_count(self):
        while self.perform_human_detection:
            time.sleep(5)
            Logger.logger().info("[INFO D 1]: {}".format(SendReceiveMessages().get_total_face_detected_count()))
            Logger.logger().info("[INFO L 2]: {}".format(SendReceiveMessages().get_face_detected_count_locally()))
            Logger.logger().info("[INFO P 3]: {}".format(SendReceiveMessages().get_face_detected_by_peer()))
            Logger.logger().info(
                "method_for_comparing_local_face_detected_and_global_face_detected: Compute total faces "
                "detected by both cameras: {}".format(SendReceiveMessages().get_total_face_detected_count()))
            if SendReceiveMessages().get_total_face_detected_count() >= MAX_OCCUPANCY:
                Logger.logger().info("Please wait because the occupancy is greater than {}".format(MAX_OCCUPANCY))

    def clean_up(self):
        self.perform_human_detection = False
        SendReceiveMessages().cleanup()
        # stop the timer and display FPS information
        self.fps.stop()
        Logger.logger().debug("elapsed time: {:.2f}".format(self.fps.elapsed()))
        Logger.logger().debug("approx. FPS: {:.2f}".format(self.fps.fps()))

        # Close the log file.
        HumanValidator.close_log_file()

        # close any open windows
        cv2.destroyAllWindows()

        # clean up
        Logger.logger().debug("cleaning up...")
        if self.find_humans_from_video_file_name:
            self.video_stream.release()
        else:
            self.video_stream.stop()
        time.sleep(2)

    def thread_for_face_tracker(self):
        return_value = True
        while self.perform_human_detection:
            try:
                self.loop_over_streams()
            except Exception as e:
                Logger.logger().error("Caught an exception while looping over streams {}, rebooting....".format(
                    type(e).__name__ + ': ' + str(e)))
                return_value = False
        return return_value
Example #6
0
#MAIN
if __name__ == '__main__':
    faceDetectionSchema = cv2.CascadeClassifier(faceHaarCascade)
    eyeDetectionSchema = cv2.CascadeClassifier(eyeHaarCascarde)

    # Video Capture CV2 method
    # Value zero reflects default video capture
    # First try call imutils library VideoStream: Much more stable than
    # native CV2 library video capture
    try:
        cam = VideoStream().start()
    except:
        deviceNum = 0
        cam = cv2.VideoCapture(deviceNum)
        if not (cam.isOpened()):
            print("\n[INFO] Video device could not be opened\n")
            exit(1)

    while True:
        img = cam.read()
        img = imutils.resize(img, width=400)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        #gray = cv2.equalizeHist(gray)

        rects = detection(gray, faceDetectionSchema)
        vis = img.copy()  #Make a frame copy for reference

        color = (11, 134, 184)  #Goldenrod
        #Color is in BGR color, RGB backwards
        drawDetectBox(img, rects, color)