# import gradio as gr
# import cv2
# import numpy as np
# import onnxruntime as ort
# # Load the ONNX model using onnxruntime
# onnx_model_path = "Model_IV.onnx" # Update with your ONNX model path
# session = ort.InferenceSession(onnx_model_path)
# # Function to perform object detection with the ONNX model
# def detect_objects(frame, confidence_threshold=0.5):
# # Convert the frame from BGR (OpenCV) to RGB
# image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# # Preprocessing: Resize and normalize the image
# # Assuming YOLO model input is 640x640, update according to your model's input size
# input_size = (640, 640)
# image_resized = cv2.resize(image, input_size)
# image_normalized = image_resized / 255.0 # Normalize to [0, 1]
# image_input = np.transpose(image_normalized, (2, 0, 1)) # Change to CHW format
# image_input = np.expand_dims(image_input, axis=0).astype(np.float32) # Add batch dimension
# # Perform inference
# inputs = {session.get_inputs()[0].name: image_input}
# outputs = session.run(None, inputs)
# # # Assuming YOLO model outputs are in the form of [boxes, confidences, class_probs]
# # boxes, confidences, class_probs = outputs
# # # Post-processing: Filter boxes by confidence threshold
# # detections = []
# # for i, confidence in enumerate(confidences[0]):
# # if confidence >= confidence_threshold:
# # x1, y1, x2, y2 = boxes[0][i]
# # class_id = np.argmax(class_probs[0][i]) # Get class with highest probability
# # detections.append((x1, y1, x2, y2, confidence, class_id))
# # # Draw bounding boxes and labels on the image
# # for (x1, y1, x2, y2, confidence, class_id) in detections:
# # color = (0, 255, 0) # Green color for bounding boxes
# # cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
# # label = f"Class {class_id}: {confidence:.2f}"
# # cv2.putText(image, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
# # # Convert the image back to BGR for displaying in Gradio
# # image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
# return outputs
# # Gradio interface to use the webcam for real-time object detection
# # Added a slider for the confidence threshold
# iface = gr.Interface(fn=detect_objects,
# #inputs=[
# # gr.Video(sources="webcam", type="numpy"), # Webcam input
# inputs = gr.Image(sources=["webcam"], type="numpy"),
# # gr.Slider(minimum=0.0, maximum=1.0, default=0.5, label="Confidence Threshold") # Confidence slider
# # ],
# outputs="image") # Show output image with bounding boxes
# iface.launch()
###
# import gradio as gr
# import cv2
# from huggingface_hub import hf_hub_download
# from gradio_webrtc import WebRTC
# from twilio.rest import Client
# import os
# from inference import YOLOv8
# model_file = hf_hub_download(
# repo_id="aje6/ASL-Fingerspelling-Detection", filename="onnx/Model_IV.onnx"
# )
# model = YOLOv8(model_file)
# account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
# auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
# if account_sid and auth_token:
# client = Client(account_sid, auth_token)
# token = client.tokens.create()
# rtc_configuration = {
# "iceServers": token.ice_servers,
# "iceTransportPolicy": "relay",
# }
# else:
# rtc_configuration = None
# def detection(image, conf_threshold=0.3):
# image = cv2.resize(image, (model.input_width, model.input_height))
# new_image = model.detect_objects(image, conf_threshold)
# return cv2.resize(new_image, (500, 500))
# css = """.my-group {max-width: 600px !important; max-height: 600 !important;}
# .my-column {display: flex !important; justify-content: center !important; align-items: center !important};"""
# with gr.Blocks(css=css) as demo:
# gr.HTML(
# """
#
# YOLOv10 Webcam Stream (Powered by WebRTC ⚡️)
#
# """
# )
# gr.HTML(
# """
#
# """
# )
# with gr.Column(elem_classes=["my-column"]):
# with gr.Group(elem_classes=["my-group"]):
# image = WebRTC(label="Stream", rtc_configuration=rtc_configuration)
# conf_threshold = gr.Slider(
# label="Confidence Threshold",
# minimum=0.0,
# maximum=1.0,
# step=0.05,
# value=0.30,
# )
# image.stream(
# fn=detection, inputs=[image, conf_threshold], outputs=[image], time_limit=10
# )
# if __name__ == "__main__":
# demo.launch()
# import gradio as gr
# import numpy as np
# import cv2
# from ultralytics import YOLO
# model = YOLO('Model_IV.pt')
# def transform_cv2(frame, transform):
# if transform == "cartoon":
# # prepare color
# img_color = cv2.pyrDown(cv2.pyrDown(frame))
# for _ in range(6):
# img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
# img_color = cv2.pyrUp(cv2.pyrUp(img_color))
# # prepare edges
# img_edges = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
# img_edges = cv2.adaptiveThreshold(
# cv2.medianBlur(img_edges, 7),
# 255,
# cv2.ADAPTIVE_THRESH_MEAN_C,
# cv2.THRESH_BINARY,
# 9,
# 2,
# )
# img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)
# # combine color and edges
# img = cv2.bitwise_and(img_color, img_edges)
# return img
# elif transform == "edges":
# # perform edge detection
# img = cv2.cvtColor(cv2.Canny(frame, 100, 200), cv2.COLOR_GRAY2BGR)
# return img
# else:
# return np.flipud(frame)
# with gr.Blocks() as demo:
# with gr.Row():
# with gr.Column():
# transform = gr.Dropdown(choices=["cartoon", "edges", "flip"],
# value="flip", label="Transformation")
# input_img = gr.Image(sources=["webcam"], type="numpy")
# with gr.Column():
# output_img = gr.Image(streaming=True)
# dep = input_img.stream(transform_cv2, [input_img, transform], [output_img],
# time_limit=30, stream_every=0.1, concurrency_limit=30)
# if __name__ == "__main__":
# demo.launch()
###
# import gradio as gr
# import torch
# import cv2
# # Load the YOLOv8 model
# model = torch.hub.load('ultralytics/yolov8', 'yolov8s', trust_repo=True)
# model.load_state_dict(torch.load('Model_IV'))
# def inference(img):
# results = model(img)
# annotated_img = results.render()[0]
# return annotated_img
# iface = gr.Interface(fn=inference, inputs="webcam", outputs="image")
# iface.launch()
import gradio as gr
import torch
from PIL import Image
import torchvision.transforms as T
from ultralytics import YOLO
# Load your model
model = YOLO()
# model = torch.load("Model_IV.pt")
# model.eval()
checkpoint = torch.load("Model_IV.pt")
# model.load_state_dict(checkpoint) # Load the saved weights
# model.eval() # Set the model to evaluation mode
# from ultralytics import settings
# # Update multiple settings
# settings.update({
# "names": {0: 'A', 1: 'B',
# 2: 'C', 3: 'D',
# 4: 'E', 5: 'F',
# 6: 'G', 7: 'H',
# 8: 'I', 9: 'J',
# 10: 'K', 11: 'L',
# 12: 'M', 13: 'N',
# 14: 'O', 15: 'P',
# 16: 'Q', 17: 'R',
# 18: 'S', 19: 'T',
# 20: 'U', 21: 'V',
# 22: 'W', 23: 'X',
# 24: 'Y', 25: 'Z'},
# "tensorboard": False
# })
# print(type(checkpoint))
# if isinstance(checkpoint, dict):
# print(checkpoint.keys())
# Define preprocessing
transform = T.Compose([
T.Resize((224, 224)), # Adjust to your model's input size
T.ToTensor(),
])
def predict(image):
# Preprocess the image
img_tensor = transform(image).unsqueeze(0) # Add batch dimension
# Make prediction
with torch.no_grad():
output = model(img_tensor)
# Process output (adjust based on your model's format)
# return output # or post-process the results as needed
results = model(image, save=True)
annotated_img = Image.load("")
return annotated_img
# Gradio interface
demo = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"), # Accepts image input
outputs="image" # Customize based on your output format
)
if __name__ == "__main__":
demo.launch()