Exemple #1
0
def main():
    # Initialize some variables
    face_locations = []
    process_this_frame = True

    # Create a new named window
    kWinName = "Face detection demo"

    # Open a video file or an image file or a camera stream
    cap = cv.VideoCapture(args.input if args.input else 0)

    while cv.waitKey(1) < 0:
        # Save program start time
        start_time = time.time()

        # Read frame
        hasFrame, frame = cap.read()
        if not hasFrame:
            cv.waitKey()
            break

        # Resize frame of video to 1/4 size for faster face recognition processing
        small_frame = cv.resize(frame, (0, 0), fx=args.scale, fy=args.scale)

        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb_small_frame = small_frame[:, :, ::-1]

        if args.skip:
            # Only process every other frame of video to save time
            if process_this_frame:
                # Find all the faces and face encodings in the current frame of video
                face_locations = face_recognition.face_locations(
                    rgb_small_frame)
            process_this_frame = not process_this_frame
        else:
            face_locations = face_recognition.face_locations(rgb_small_frame)

        # Display the results
        drawDetection(frame, face_locations, args.scale)

        # Calculate processing time
        label = "Process time: %.2f ms" % ((time.time() - start_time) * 1000)
        print("[INFO] " + label)
        if args.info:
            cv.putText(frame, label, (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5,
                       (0, 255, 255))

        # Display the frame
        cv.imshow(kWinName, frame)

        # Save results
        if args.save and args.input:
            saveResult(frame, "detection")

    # Release handle to the webcam
    cap.release()
    cv.destroyAllWindows()
Exemple #2
0
def main():
    # pytesseract config
    config = "-l eng --oem 1 --psm 3"

    # Create a new named window
    kWinName = "OCR demo with tesseract"

    # Open a video file or an image file or a camera stream
    cap = cv.VideoCapture(args.input if args.input else 0)

    while cv.waitKey(1) < 0:
        # Save program start time
        start_time = time.time()

        # Read frame
        hasFrame, frame = cap.read()
        if not hasFrame:
            cv.waitKey()
            break

        # Put OCR information
        text = pytesseract.image_to_string(frame, config=config)

        if len(text) > 0:
            cv.putText(frame, text, (0, 60), cv.FONT_HERSHEY_SIMPLEX, 0.5,
                       (0, 255, 255))
            print("[INFO] text '{}' found".format(text))
        else:
            print("[INFO] No text found")

        # Calculate processing time
        label = "Total process time: %.2f ms" % (
            (time.time() - start_time) * 1000)
        print("[INFO] " + label)
        if args.info:
            cv.putText(frame, label, (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5,
                       (0, 255, 255))

        # Display the frame
        cv.imshow(kWinName, frame)

        # Save results
        if args.save and args.input:
            saveResult(frame, "tess")
Exemple #3
0
def main():
    # Read and store arguments
    confThreshold = args.thr
    nmsThreshold = args.nms
    inpWidth = args.width
    inpHeight = args.height

    # Load network
    net = cv.dnn.readNet(args.model)
    print("[INFO] East model loaded from {}".format(args.model))

    # Create a new named window
    kWinName = "Text detection demo with EAST"
    outNames = []
    outNames.append("feature_fusion/Conv_7/Sigmoid")
    outNames.append("feature_fusion/concat_3")

    # Open a video file or an image file or a camera stream
    cap = cv.VideoCapture(args.input if args.input else 0)

    while cv.waitKey(1) < 0:
        # Save program start time
        start_time = time.time()

        # Read frame
        hasFrame, frame = cap.read()
        if not hasFrame:
            cv.waitKey()
            break

        # Get frame height and width
        height_ = frame.shape[0]
        width_ = frame.shape[1]
        rW = width_ / float(inpWidth)
        rH = height_ / float(inpHeight)

        # Create a 4D blob from frame.
        blob = cv.dnn.blobFromImage(
            frame,
            1.0,
            (inpWidth, inpHeight),
            (123.68, 116.78, 103.94),
            swapRB=True,
            crop=False,
        )

        # Run the model
        net.setInput(blob)
        outs = net.forward(outNames)
        t, _ = net.getPerfProfile()
        label = "Inference time: %.2f ms" % (t * 1000.0 / cv.getTickFrequency())

        # Get scores and geometry
        scores = outs[0]
        geometry = outs[1]
        [boxes, confidences] = decode(scores, geometry, confThreshold)

        # Draw results
        drawResult(frame, rW, rH, boxes, confidences, confThreshold, nmsThreshold)

        # Put efficiency information
        print("[INFO] EAST " + label)
        if args.info:
            cv.putText(
                frame, label, (0, 60), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255)
            )

        # Calculate processing time
        label = "Total process time: %.2f ms" % ((time.time() - start_time) * 1000)
        print("[INFO] " + label)
        if args.info:
            cv.putText(
                frame, label, (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255)
            )

        # Display the frame
        cv.imshow(kWinName, frame)

        # Save results
        if args.save and args.input:
            saveResult(frame, "text_east")
Exemple #4
0
def main():
    # Read and store arguments
    confThreshold = args.thr
    nmsThreshold = args.nms
    inpWidth = args.width
    inpHeight = args.height

    # Load network
    net = cv.dnn.readNet(args.model)
    print("[INFO] East model loaded from {}".format(args.model))

    # Start RealSense Camera
    options = rsOptions()
    options.enableColor = True
    options.resColor = [1280, 720]
    rs = realsense(options)
    rs.deviceInitial()

    # Create a new named window
    kWinName = "Text detection demo with EAST"
    outNames = []
    outNames.append("feature_fusion/Conv_7/Sigmoid")
    outNames.append("feature_fusion/concat_3")

    flagCapture = False

    try:
        while True:
            # Save program start time
            start_time = time.time()

            # Read frame
            rs.getFrame()
            frame = rs.imageColor
            if not frame.any():
                cv.waitKey()
                break

            # Get frame height and width
            height_ = frame.shape[0]
            width_ = frame.shape[1]
            rW = width_ / float(inpWidth)
            rH = height_ / float(inpHeight)

            # Create a 4D blob from frame.
            blob = cv.dnn.blobFromImage(
                frame,
                1.0,
                (inpWidth, inpHeight),
                (123.68, 116.78, 103.94),
                swapRB=True,
                crop=False,
            )

            # Run the model
            net.setInput(blob)
            outs = net.forward(outNames)
            t, _ = net.getPerfProfile()
            label = "Inference time: %.2f ms" % (t * 1000.0 / cv.getTickFrequency())

            # Get scores and geometry
            scores = outs[0]
            geometry = outs[1]
            [boxes, confidences] = decode(scores, geometry, confThreshold)

            # Draw results
            drawResult(frame, rW, rH, boxes, confidences, confThreshold, nmsThreshold)

            # Put efficiency information
            if args.info:
                cv.putText(
                    frame, label, (0, 60), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255)
                )

            # Calculate processing time
            label = "Total process time: %.2f ms" % ((time.time() - start_time) * 1000)
            if args.info:
                cv.putText(
                    frame, label, (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255)
                )

            # Display the frame
            cv.imshow(kWinName, frame)

            # Process screen capture
            if flagCapture:
                print("[INFO] Screen captured")
                saveResult(frame, "text_rs_east")
                flagCapture = False

            # Keyboard commands
            getKey = cv.waitKey(1) & 0xFF
            if getKey is ord("c") or getKey is ord("C"):
                flagCapture = True
            elif getKey is ord("q") or getKey is ord("Q"):
                break

    except Exception as e:
        print(e)
        pass

    finally:
        # Stop streaming
        cv.destroyAllWindows()
        rs.pipeline.stop()
Exemple #5
0
def main():
    # load learned faces
    print("[INFO] loading faces ...")
    # check the image source comes from
    print("[INFO] faces loaded from {} ...".format(args.pickle))
    data = pickle.loads(open(args.pickle, "rb").read())

    # Initialize some variables
    face_locations = []
    process_this_frame = True
    flagCapture = False

    # Create a new named window
    kWinName = "Face recognition demo"

    # Start RealSense Camera
    options = rsOptions()
    options.enableColor = True
    options.resColor = [1280, 720]
    rs = realsense(options)
    rs.deviceInitial()

    try:
        while True:
            # Save program start time
            if args.skip is True:
                if process_this_frame:
                    start_time = time.time()
            else:
                start_time = time.time()

            # Read frame
            rs.getFrame()
            frame = rs.imageColor
            if not frame.any():
                cv.waitKey()
                break

            # Resize frame of video to 1/2 size for faster face recognition processing
            small_frame = cv.resize(frame, (0, 0),
                                    fx=args.scale,
                                    fy=args.scale)

            # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
            rgb_small_frame = small_frame[:, :, ::-1]

            if args.skip is True:
                # Only process every other frame of video to save time
                if process_this_frame:
                    face_locations, face_names = faceMatch(
                        rgb_small_frame, data, args.threshold)
                process_this_frame = not process_this_frame
            else:
                face_locations, face_names = faceMatch(rgb_small_frame, data,
                                                       args.threshold)

            # Display the results
            drawRecognition(frame, face_locations, face_names, args.scale)

            # Calculate processing time
            if args.skip is True:
                if not process_this_frame:
                    label = "Process time: %.2f ms" % (
                        (time.time() - start_time) * 500)
            else:
                label = "Process time: %.2f ms" % (
                    (time.time() - start_time) * 1000)

            # Display infomation
            if args.info is True:
                # if not process_this_frame:
                # print("[INFO] " + label)
                cv.putText(frame, label, (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5,
                           (0, 255, 255))

            # Display the frame
            cv.imshow(kWinName, frame)

            # Process screen capture
            if flagCapture:
                print("[INFO] Screen captured")
                saveResult(frame, "recognition_rs")
                flagCapture = False

            # Keyboard commands
            getKey = cv.waitKey(1) & 0xFF
            if getKey is ord("c") or getKey is ord("C"):
                flagCapture = True
            elif getKey is ord("q") or getKey is ord("Q"):
                break

    except Exception as e:
        print(e)
        pass

    finally:
        # Stop streaming
        cv.destroyAllWindows()
        rs.pipeline.stop()
Exemple #6
0
def main():
    # load learned faces
    print("[INFO] loading faces ...")
    # check the image source comes from
    print("[INFO] faces loaded from {} ...".format(args.pickle))
    data = pickle.loads(open(args.pickle, "rb").read())

    # Initialize some variables
    face_locations = []
    process_this_frame = True

    # Create a new named window
    kWinName = "Face recognition demo"

    # Open a video file or an image file or a camera stream
    cap = cv.VideoCapture(args.input if args.input else 0)

    while cv.waitKey(1) < 0:
        # Save program start time
        start_time = time.time()

        # Read frame
        hasFrame, frame = cap.read()
        if not hasFrame:
            cv.waitKey()
            break

        # Resize frame of video to 1/2 size for faster face recognition processing
        small_frame = cv.resize(frame, (0, 0), fx=args.scale, fy=args.scale)

        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb_small_frame = small_frame[:, :, ::-1]

        if args.skip:
            # Only process every other frame of video to save time
            if process_this_frame:
                face_locations, face_names = faceMatch(rgb_small_frame, data,
                                                       args.threshold)
                if args.landmark:
                    face_landmarks = face_recognition.face_landmarks(
                        rgb_small_frame, face_locations)
            process_this_frame = not process_this_frame
        else:
            face_locations, face_names = faceMatch(rgb_small_frame, data,
                                                   args.threshold)
            if args.landmark:
                face_landmarks = face_recognition.face_landmarks(
                    rgb_small_frame, face_locations)

        if args.landmark:
            drawLandmarks(frame, face_landmarks, args.scale)

        # Display the results
        drawRecognition(frame, face_locations, face_names, args.scale)

        # Calculate processing time
        label = "Process time: %.2f ms" % ((time.time() - start_time) * 1000)
        print("[INFO] " + label)
        if args.info:
            cv.putText(frame, label, (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5,
                       (0, 255, 255))

        # Display the frame
        cv.imshow(kWinName, frame)

        # Save results
        if args.save and args.input:
            saveResult(frame, "recognition")

    # Release handle to the webcam
    cap.release()
    cv.destroyAllWindows()
Exemple #7
0
def main():
    # load svm model
    print("[INFO] loading SVM model ...")
    print("[INFO] SVM model from {} ...".format(args.svm))
    clf = pickle.loads(open(args.svm, "rb").read())

    # Initialize some variables
    face_locations = []
    process_this_frame = True

    # Create a new named window
    kWinName = "Face recognition demo with SVM classifier"

    # Open a video file or an image file or a camera stream
    cap = cv.VideoCapture(args.input if args.input else 0)

    while cv.waitKey(1) < 0:
        # Save program start time
        start_time = time.time()

        # Read frame
        hasFrame, frame = cap.read()
        if not hasFrame:
            cv.waitKey()
            break

        # Resize frame of video to 1/2 size for faster face recognition processing
        small_frame = cv.resize(frame, (0, 0), fx=args.scale, fy=args.scale)

        # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
        rgb_small_frame = small_frame[:, :, ::-1]

        if args.skip:
            # Only process every other frame of video to save time
            if process_this_frame:
                # Find all the faces and face encodings in the current frame of video
                face_locations = face_recognition.face_locations(
                    rgb_small_frame)
                face_encodings = face_recognition.face_encodings(
                    rgb_small_frame, face_locations)

                face_names = []
                for face_encoding in face_encodings:
                    # predict face by SVM
                    name = clf.predict([face_encoding])
                    face_names.append(name[0])

            process_this_frame = not process_this_frame
        else:
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(
                rgb_small_frame, face_locations)

            face_names = []
            for face_encoding in face_encodings:
                # predict face by SVM
                name = clf.predict([face_encoding])
                face_names.append(name[0])

        # Display the results
        drawRecognition(frame, face_locations, face_names, args.scale)

        # Calculate processing time
        label = "Process time: %.2f ms" % ((time.time() - start_time) * 1000)
        print("[INFO] " + label)
        if args.info:
            cv.putText(frame, label, (0, 30), cv.FONT_HERSHEY_SIMPLEX, 0.5,
                       (0, 255, 255))

        # Display the frame
        cv.imshow(kWinName, frame)

        # Save results
        if args.save and args.input:
            saveResult(frame, "recognition_svm")

    # Release handle to the webcam
    cap.release()
    cv.destroyAllWindows()