Importing Tensorflow Causes Program to Freeze

Sam*_*ner 5 python memory-leaks keras tensorflow mediapipe

I'm trying to run a hand analysis program using mediapipe's hand recognition library as well as tensorflow models to recognize if hands are in a particular position. I tried to use the two together, but ran into an issue.

Whenever I try to use both the tensorflow model and the mediapipe library, I cannot. The program freezes and does not run to completion. However, as soon as I remove the model loading, the program runs just fine. So I'm wondering if there's some sort of memory issue that's holding things up, which would be odd, as the model I'm trying to load is only 25kb.

Here's the code:

import cv2
from tensorflow.keras.models import load_model
import h5py
import mediapipe as mp

model = load_model('/path_to_model/model.h5')

print('Marker 1')

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

print('Marker 2')

cap = cv2.VideoCapture(0)
cap.set(3, 1280) # set the Horizontal resolution
cap.set(4, 720) # Set the Vertical resolution

print('Marker 3')

num_frames = 0

print('Marker 4')

with mp_hands.Hands(
    min_detection_confidence=.5,
    min_tracking_confidence=.1) as hands:
    print('Marker 5')
    while cap.isOpened():
        print('Marker 6')
        success, image = cap.read()
        print('Marker 7')
        if not success:
            print("Ignoring empty camera frame.")
            continue
        print('Marker 8')

        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        print('Marker 9')

        gray_roi =cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
        
        print('Marker 10')
        
        image.flags.writeable = False
        results = hands.process(image)
        print('Marker 11')

        image.flags.writeable = True
        print('Marker 12')
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        print('Marker 13')
        if results.multi_hand_landmarks:
            print('Marker 14')
            for hand_landmarks in results.multi_hand_landmarks:
                print('Marker 15')
                mp_drawing.draw_landmarks(
                image,
                hand_landmarks,
                mp_hands.HAND_CONNECTIONS,
                mp_drawing_styles.get_default_hand_landmarks_style(),
                mp_drawing_styles.get_default_hand_connections_style())
                print('Marker 16')
        print('Marker 17')
        if results.multi_handedness is None:
                print('Marker 17')
                string = 'Handedness: N/A'
        else:
            print('Marker 18')
            string = 'Handedness:' + str(len(results.multi_handedness))
      
            print('Marker 19')
            if num_frames % 5 == 0:
                    print('Marker 20')
                    position = function_that_uses_model_to_determine_hand_position(gray_roi)  
                    print('Marker 21')
                
        print('Marker 22')    
        cv2.putText(image, string, (10, 70), cv2.FONT_HERSHEY_PLAIN, 3,
        (255, 0, 255), 3)
        cv2.putText(image, position, (70, 70), cv2.FONT_HERSHEY_PLAIN, 3,
        (255, 0, 255), 3)
        print('Marker 23')
        cv2.imshow('MediaPipe Hands', image)
        print('Marker 24')
        if cv2.waitKey(5) & 0xFF == 27:
            break
        print('Marker 25')
        num_frames+=1
        print('Marker 26')
cap.release()
Run Code Online (Sandbox Code Playgroud)

如果我注释掉 load_model 部分(以及相关的代码行),它运行得很好。然而,每当我尝试包含加载模型时,我只会将其添加到Marker 10. 哎呀,我什至不需要尝试加载模型。Marker 10如果我只包含该行而from tensorflow.keras.models import load_model与张量流无关,我仍然只能做到这一点。显然,导入或使用它会导致一些问题,从而阻止程序的其余部分运行。

我的tensorflow版本是1.14.0,keras版本是2.3.1,python版本是3.7.6。

如果聪明人知道如何解决这个问题,请告诉我!

谢谢,萨姆

Var*_* W. 1

我发现你的问题不是代码,而是张量流本身,你使用的版本是从 2019 年 6 月到 2019 年 9 月使用的。简而言之,你的代码是正确的,但你使用的张量流版本是错误的,我希望这会有所帮助。

\n

2019 年 7 月,这篇文章发表,称 TensorFlow 无缘无故崩溃:

\n

https://discuss.python.org/t/crashes-combining-tensorflow-with-other-packages-who-can-reproduct/2029#:~:text=The%20way%20Tensorflow%20built%20their%20\xe2 \x80\x9cmanylinux\xe2\x80\x9d%20wheels%20made,part%20of%20the%20people%20who%20looked%20into%20it)。

\n

在此输入图像描述

\n

如果您认为我已经回答了您的问题,请将其标记为答案

\n