from transformers import ViTImageProcessor, AutoModelForImageClassification import torch import gradio as gr import os import glob import mediapipe as mp import numpy as np from PIL import Image feature_extractor = ViTImageProcessor.from_pretrained('ArdyL/VIT_SIBI_ALL') model = AutoModelForImageClassification.from_pretrained('ArdyL/VIT_SIBI_ALL') mp_drawing_styles = mp.solutions.drawing_styles mp_holistic = mp.solutions.holistic mp_pose = mp.solutions.pose mp_drawing = mp.solutions.drawing_utils def preprocess(im): with mp_holistic.Holistic( static_image_mode=True, min_detection_confidence=0.3, model_complexity=2) as hands: # Read image file with cv2 and process with face_mesh results = hands.process(im) image2 = np.array(im) annotated_image = image2.copy() annotated_image = np.empty(annotated_image.shape) annotated_image.fill(255) if results.pose_landmarks: mp_drawing.draw_landmarks(annotated_image, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS, mp_drawing.DrawingSpec( color=(0, 0, 0), thickness=2, circle_radius=2), mp_drawing.DrawingSpec( color=(0, 0, 0), thickness=2, circle_radius=2), ) mp_drawing.draw_landmarks(annotated_image, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS, mp_drawing.DrawingSpec( color=(0, 0, 0), thickness=2, circle_radius=2), mp_drawing.DrawingSpec( color=(0, 0, 0), thickness=2, circle_radius=2), ) mp_drawing.draw_landmarks(annotated_image, results.face_landmarks, mp_holistic.FACEMESH_TESSELATION, landmark_drawing_spec=None, connection_drawing_spec=mp_drawing_styles .get_default_face_mesh_tesselation_style() ) mp_drawing.draw_landmarks(annotated_image, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS, mp_drawing.DrawingSpec( color=(0, 0, 0), thickness=2, circle_radius=2), mp_drawing.DrawingSpec( color=(0, 0, 0), thickness=2, circle_radius=2), ) annotated_image[...] /= 255 return annotated_image def classify_image(image): preprocessedImage = preprocess(image) with torch.no_grad(): model.eval() inputs = feature_extractor( images=preprocessedImage, return_tensors="pt") outputs = model(**inputs) logits = outputs.logits predicted_label = logits.argmax(-1).item() label = model.config.id2label[predicted_label] # prob = torch.nn.functional.softmax(logits, dim=1) # top10_prob, top10_indices = torch.topk(prob, 5) # top10_confidences = {} # for i in range(5): # top10_confidences[model.config.id2label[int( # top10_indices[0][i])]] = float(top10_prob[0][i]) return label # confidences with gr.Blocks(title=">ViT - SIBI Classifier" ) as demo: # gr.HTML("""