TrashDetection / model.py
Thanh Nguyen
...
bdad9c3
Raw History Blame Contribute Delete
3.42 kB
import gradio as gr
import cv2
import tempfile
from ultralytics import YOLO
import io
import base64
from utils import get_coordinates_from_exif, covert_image_tobase64,get_adress
def yolov10_inference(collection,image, video, model_id, image_size, conf_threshold):
model = YOLO(f'runs/{model_id}.pt')
lat, lon,date_create = get_coordinates_from_exif(image)
# if lat is None or lon is None:
# gr.Warning('Không tìm thấy tọa độ GPS trong hình ảnh')
# return "Không tìm thấy tọa độ GPS trong hình ảnh", None
if image:
results = model.predict(source=image, imgsz=(640,640), conf=conf_threshold,agnostic_nms=True, show_conf=False)
bboxes = results[0].boxes.xywh.cpu().numpy().tolist()
clss = results[0].boxes.cls.cpu().numpy().tolist()
annotated_image = results[0].plot()
img_str, im_rgb = covert_image_tobase64(annotated_image)
# Lưu trữ hình ảnh và bbox vào MongoDB
if len(bboxes) > 0 and lat is not None and lon is not None and model_id == 'Yolov8n_4':
address = get_adress(lat, lon)
collection.update_one({"image": img_str},
{ "$set": {"image": img_str, "latitude": lat, "longitude": lon, "bboxes": bboxes, "class":clss, "address":address,"date_create":date_create}},
True)
# return annotated_image[:, :, ::-1], None
return im_rgb, None
else:
# video_path = tempfile.mktemp(suffix=".webm")
# with open(video_path, "wb") as f:
# with open(video, "rb") as g:
# f.write(g.read())
cap = cv2.VideoCapture(video)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
output_video_path = tempfile.mktemp(suffix=".webm")
out = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*'vp80'), fps, (frame_width, frame_height))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
results = model.track(frame, persist=True, conf=0.5, imgsz=(640,640))
# Check if tracking IDs are available
if hasattr(results[0].boxes, 'id') and results[0].boxes.id is not None:
annotated_frame = results[0].plot()
boxes = results[0].boxes.xyxy.cpu().numpy().astype(int)
ids = results[0].boxes.id.cpu().numpy().astype(int)
conf = max(results[0].boxes.conf.cpu().numpy())
lat, lon,date_create = get_coordinates_from_exif(frame)
out.write(annotated_frame)
# re None, output_video_path,annotated_frame
# results[0].save(filename=f'img_result/VID_20240513_171727_{ids[0]}_{conf}.jpg')
# # Draw boxes and IDs on the frame
# for box, id in zip(boxes, ids):
# cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
# cv2.putText(frame, f"Id {id}", (box[0], box[1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
else:
out.write(frame)
print("Tracking IDs not available.")
cap.release()
out.release()
return None, output_video_path