Tuesday, 5 October 2021

Importing Tensorflow Causes Program to Freeze

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()

If I comment the load_model parts out (and the associated code lines), it runs just fine. However, whenever I try to include loading the model, I only make it to Marker 10. Heck, I don't even need to try loading the model. I still only make it to Marker 10 if all I include is the from tensorflow.keras.models import load_model line and nothing else to do with tensorflow. So obviously importing or using that is causing some issues that prevent the rest of the program from running.

My tensorflow version is 1.14.0, keras version is 2.3.1, and python version is 3.7.6.

Let me know if you smart people know how this can be remedied!

Thanks, Sam



from Importing Tensorflow Causes Program to Freeze

No comments:

Post a Comment