mediapipe icon indicating copy to clipboard operation
mediapipe copied to clipboard

detector.detect_async wouldn't be used with drawing bounding boxes

Open Foundsheep opened this issue 1 year ago • 0 comments

Have I written custom code (as opposed to using a stock example script provided in MediaPipe)

No

OS Platform and Distribution

Windows 11

MediaPipe Tasks SDK version

latest

Task name (e.g. Image classification, Gesture recognition etc.)

Face Detection

Programming Language and version (e.g. C++, Python, Java)

Python

Describe the actual behavior

detector.detect_async for live stream mode is giving None all the time, when trying to use the result object

Describe the expected behaviour

expected to be returned with FaceDetectorResult object, when having call detect_async so that I could use bbox information to draw in the image

Standalone code/steps you may have used to try to get what you need

def capture(name):
    now = datetime.datetime.now()
    timestamp = now.strftime('%Y%m%d_%H%M%S')
    # timestamp_ms = int(time.time() * 1000)

    face_detector_options = get_face_detector_options()
    with FaceDetector.create_from_options(face_detector_options) as detector:
        cap = cv2.VideoCapture(0)

        count = 0
        ms = 0

        save_dir = Path(f"./{name}_{timestamp}")
        if not save_dir.exists():
            save_dir.mkdir()
            print(f"[{save_dir}] made!!!!!!!!!!!")

        while cap.isOpened():
            success, image = cap.read()
            if not success:
                print("Ignoring empty camera frame.")
                continue
            mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=image)
            ms += 1
            results = detector.detect_async(mp_image, ms)
            image_copy = image.copy()
            
            # found results is always None here
            if results is not None:
                for detection in results.detections:
                    bbox = detection.bounding_box
                    x = bbox.origin_x
                    y = bbox.origin_y
                    h = bbox.height
                    w = bbox.width
                    cropped = image[y:y+h, x:x+w, :]

                    cv2.rectangle(image_copy, (x, y), (x+w, y+h), COLOR, 10)
                
            else:
                print("not found.....")

            count += 1
            cv2.imwrite(f"{save_dir.absolute()}/{str(count).zfill(4)}.png", image_copy)
            cv2.imshow("image captured", cv2.flip(image_copy, 1))
            if cv2.waitKey(5) & 0xFF == 27:
                break
        cap.release()
        print("DONE")

Other info / Complete Logs

No response

Foundsheep avatar Jul 10 '24 08:07 Foundsheep