input video size is limited

This commit is contained in:
Sencer Yücel 2024-05-31 14:29:06 +03:00
parent 34af6d27fa
commit 1771746f62

26
app.py
View File

@ -6,14 +6,24 @@ from ultralytics import YOLOv10
import cv2 import cv2
import tempfile import tempfile
def yolov10_inference(image, model_path, image_size, conf_threshold):
def check_file_size(file, max_file_size=20*1024*1024): # 20MB
file_size = 0
if file:
with open(file, "rb") as f:
file_size = len(f.read())
if file_size > max_file_size:
raise gr.Error("File size exceeds the 20MB limit. Please try with another file.")
def yolov10_inference(image, video, model_path, image_size, conf_threshold):
model = YOLOv10(model_path) model = YOLOv10(model_path)
if image:
results = model.predict(source=image, imgsz=image_size, conf=conf_threshold) results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
annotated_image = results[0].plot() annotated_image = results[0].plot()
return annotated_image[:, :, ::-1], None return annotated_image[:, :, ::-1], None
else:
def yolov10_inference_video(video, model_path, image_size, conf_threshold): check_file_size(video)
model = YOLOv10(model_path)
video_path = tempfile.mktemp(suffix=".mp4") video_path = tempfile.mktemp(suffix=".mp4")
with open(video_path, "wb") as f: with open(video_path, "wb") as f:
with open(video, "rb") as g: with open(video, "rb") as g:
@ -41,10 +51,12 @@ def yolov10_inference_video(video, model_path, image_size, conf_threshold):
return None, output_video_path return None, output_video_path
def yolov10_inference_for_examples(image, model_path, image_size, conf_threshold): def yolov10_inference_for_examples(image, model_path, image_size, conf_threshold):
annotated_image, _ = yolov10_inference(image, model_path, image_size, conf_threshold) annotated_image, _ = yolov10_inference(image, None, model_path, image_size, conf_threshold)
return annotated_image return annotated_image
def app(): def app():
with gr.Blocks(): with gr.Blocks():
with gr.Row(): with gr.Row():
@ -105,9 +117,9 @@ def app():
def run_inference(image, video, model_id, image_size, conf_threshold, input_type): def run_inference(image, video, model_id, image_size, conf_threshold, input_type):
if input_type == "Image": if input_type == "Image":
return yolov10_inference(image, model_id, image_size, conf_threshold) return yolov10_inference(image, None, model_id, image_size, conf_threshold)
else: else:
return yolov10_inference_video(video, model_id, image_size, conf_threshold) return yolov10_inference(None, video, model_id, image_size, conf_threshold)
yolov10_infer.click( yolov10_infer.click(
fn=run_inference, fn=run_inference,