MediaPipe 手动检测 hands.process 永远等待

MediaPipe hands-detection hands.process waits forever

当我运行以下Python文件

import tensorflow as tf
import tensorflow_hub as hub
from tensorflow_docs.vis import embed
import numpy as np
import cv2
import json
import mediapipe as mp

mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands

def detectHands():
    print("detect hands")
    with mp_hands.Hands(
            static_image_mode=True,
            model_complexity=1,
            min_detection_confidence=0.5,
            max_num_hands=2) as hands:
        image_path_1 = './210.jpg'
        hands_image_input = tf.io.read_file(image_path_1)
        hands_image_input = tf.image.decode_jpeg(hands_image_input)
        # print("  read_file = cv2.imread(image_path_1);")
        # read_file = cv2.imread(image_path_1)
        # print("handsImage = cv2.flip(cv2.imread(input_image), 1)")
        # print(hands_image_input)
        # hands_image_input = cv2.flip(cv2.imread(image_path_1), 1)
        # Convert the BGR image to RGB before processing.
        # print("hands.process(read_file)",read_file)
        # print("hands ")
        print("hands ")
        print(hands)
        # flipped_image = cv2.cvtColor(read_file, cv2.COLOR_BGR2RGB)
        print("flipped_image ")
        # print(flipped_image)
        results = hands.process(hands_image_input)

        print('results!:', results)
        # Print handedness and draw hand landmarks on the image.
        print('Handedness:', results.multi_handedness)
        if not results.multi_hand_landmarks:
            return
        image_height, image_width, _ = image.shape
        annotated_image = image.copy()
        for hand_landmarks in results.multi_hand_landmarks:
            print('hand_landmarks:', hand_landmarks)
            print(
                f'Index finger tip coordinates: (',
                f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].x * image_width}, '
                f'{hand_landmarks.landmark[mp_hands.HandLandmark.INDEX_FINGER_TIP].y * image_height})'
            )
            mp_drawing.draw_landmarks(
                annotated_image,
                hand_landmarks,
                mp_hands.HAND_CONNECTIONS,
                mp_drawing_styles.get_default_hand_landmarks_style(),
                mp_drawing_styles.get_default_hand_connections_style())
        # cv2.imwrite(
        #     './tmp/annotated_image.png', cv2.flip(annotated_image, 1))
        # Draw hand world landmarks.
        if not results.multi_hand_world_landmarks:
            return
        for hand_world_landmarks in results.multi_hand_world_landmarks:
            mp_drawing.plot_landmarks(
                hand_world_landmarks, mp_hands.HAND_CONNECTIONS, azimuth=5)


detectHands()

它显示以下输出,但它永远不会低于下一行

 results = hands.process(hands_image_input)
/Users/richardlindhout/PycharmProjects/pythonProject1/venv/bin/python /Users/richardlindhout/go/src/bitbucket.org/omoda-applicatiebeheer/media-service/external_helpers/pythonProject1/main.py
2022-03-02 16:33:52.222319: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
detect hands
hands 
<mediapipe.python.solutions.hands.Hands object at 0x15902c2e0>
flipped_image 

为什么它没有超时或者为什么需要很长时间才能得到结果?

我认为我的 mac 安装中的 tensorflow 有问题。

我现在 运行 它与 Docker 所以我没有环境问题。

我有以下 Dockerfile


FROM tensorflow/tensorflow:latest

RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        gcc-8 g++-8 \
        ca-certificates \
        curl \
        ffmpeg \
        git \
        wget \
        unzip \
        python3-dev \
        python3-opencv \
        python3-pip \
        libopencv-core-dev \
        libopencv-highgui-dev \
        libopencv-imgproc-dev \
        libopencv-video-dev \
        libopencv-calib3d-dev \
        libopencv-features2d-dev \
        software-properties-common && \
    add-apt-repository -y ppa:openjdk-r/ppa && \
    apt-get update && apt-get install -y openjdk-8-jdk && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 100 --slave /usr/bin/g++ g++ /usr/bin/g++-8
RUN pip3 install --upgrade setuptools
RUN pip3 install tensorflow_hub
RUN pip3 install numpy
RUN pip3 install mediapipe


RUN ln -s /usr/bin/python3 /usr/bin/python

并用

构建它
docker build - < Dockerfile -t python-ai

那我就可以运行Python代码了!

docker run -it --rm -v $PWD:/tmp -w /tmp python-ai python ./main.py