Outils pour utilisateurs

Outils du site


movenet

Table des matières

MoveNet

Ressources

Exemple

from time import time
import cv2
import numpy as np
import tensorflow as tf
 
def draw(frame, keypoints_with_scores):
    """Calcul étrange"""
    for item in keypoints_with_scores[0][0]:
        if item[2] > 0.5:  # confiance
            x = int(item[1]*640)  # numpy array x est le 2ème
            y = int(item[0]*640) - 80
            cv2.circle(frame, (x, y), 6, color=(0,0,255), thickness=-1)
    return frame
 
interpreter = tf.lite.Interpreter(model_path="lite-model_movenet_singlepose_thunder_3.tflite")
interpreter.allocate_tensors()
 
cv2.namedWindow('color', cv2.WND_PROP_FULLSCREEN)
cam = cv2.VideoCapture(0)
t0 = time()
nbr = 0
 
while 1:
 
    nbr += 1
    ret, frame = cam.read()
 
    if not ret:
        continue
 
    image = tf.expand_dims(frame, axis=0)
    # Resize and pad the image to keep the aspect ratio and fit the expected size.
    image = tf.image.resize_with_pad(image, 256, 256)
 
    # TF Lite format expects tensor type of float32.
    input_image = tf.cast(image, dtype=tf.float32)
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
 
    interpreter.set_tensor(input_details[0]['index'], input_image.numpy())
    interpreter.invoke()
 
    # Output is a [1, 1, 17, 3] numpy array.
    keypoints_with_scores = interpreter.get_tensor(output_details[0]['index'])
 
    frame = draw(frame, keypoints_with_scores)
 
    # Affichage de l'image
    cv2.imshow('color', frame)
 
    # Calcul du FPS, affichage toutes les 10 s
    if time() - t0 > 10:
        print("FPS =", round(nbr/10, 2))
        t0, nbr = time(), 0
 
    # Pour quitter
    if cv2.waitKey(1) == 27:
        break
 
cv2.destroyAllWindows()

Des essais amusants

movenet.txt · Dernière modification: 2021/10/25 11:21 de serge