Aastha
Modifications in UI
cc2f831
import gradio as gr
import argparse
from pathlib import Path
import torch
from numpy import random
from models.experimental import attempt_load
from utils.datasets import LoadImages
from utils.general import check_img_size, scale_coords, xyxy2xywh, non_max_suppression
from utils.plots import plot_one_box
from PIL import Image
from huggingface_hub import hf_hub_download
import cv2
import json
import numpy as np
cameras = {
"0031_SanRafael": [
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.165, "y": 0.594 },{ "x": 0.208, "y": 0.582 },{ "x": 0.344, "y": 0.912 },{ "x": 0.238, "y": 0.916 }]},
{"coordinates": [{ "x": 0.569, "y": 0.571 },{ "x": 0.617, "y": 0.620 },{ "x": 0.663, "y": 0.719 },{ "x": 0.671, "y": 0.866 },{ "x": 0.914, "y": 0.836 },{ "x": 0.710, "y": 0.668 },{ "x": 0.581, "y": 0.579 }]}
])},
{"name": "night", "path": "night_2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.165, "y": 0.594 },{ "x": 0.208, "y": 0.582 },{ "x": 0.344, "y": 0.912 },{ "x": 0.238, "y": 0.916 }]},
{"coordinates": [{ "x": 0.569, "y": 0.571 },{ "x": 0.617, "y": 0.620 },{ "x": 0.663, "y": 0.719 },{ "x": 0.671, "y": 0.866 },{ "x": 0.914, "y": 0.836 },{ "x": 0.710, "y": 0.668 },{ "x": 0.581, "y": 0.579 }]}
])},
{"name": "day", "path": "daytime.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.165, "y": 0.594 },{ "x": 0.208, "y": 0.582 },{ "x": 0.344, "y": 0.912 },{ "x": 0.238, "y": 0.916 }]},
{"coordinates": [{ "x": 0.569, "y": 0.571 },{ "x": 0.617, "y": 0.620 },{ "x": 0.663, "y": 0.719 },{ "x": 0.671, "y": 0.866 },{ "x": 0.914, "y": 0.836 },{ "x": 0.710, "y": 0.668 },{ "x": 0.581, "y": 0.579 }]}
])},
{"name": "day", "path": "daytime2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.165, "y": 0.594 },{ "x": 0.208, "y": 0.582 },{ "x": 0.344, "y": 0.912 },{ "x": 0.238, "y": 0.916 }]},
{"coordinates": [{ "x": 0.569, "y": 0.571 },{ "x": 0.617, "y": 0.620 },{ "x": 0.663, "y": 0.719 },{ "x": 0.671, "y": 0.866 },{ "x": 0.914, "y": 0.836 },{ "x": 0.710, "y": 0.668 },{ "x": 0.581, "y": 0.579 }]}
])}
],
"0034_Novato": [
{"name": "day", "path": "day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.072, "y": 0.475 },{ "x": 0.105, "y": 0.445 },{ "x": 0.212, "y": 0.914 },{ "x": 0.027, "y": 0.911 }]},
{"coordinates": [{ "x": 0.233, "y": 0.433 },{ "x": 0.268, "y": 0.409 },{ "x": 0.879, "y": 0.885 },{ "x": 0.695, "y": 0.914 }]}
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.072, "y": 0.475 },{ "x": 0.105, "y": 0.445 },{ "x": 0.212, "y": 0.914 },{ "x": 0.027, "y": 0.911 }]},
{"coordinates": [{ "x": 0.233, "y": 0.433 },{ "x": 0.268, "y": 0.409 },{ "x": 0.879, "y": 0.885 },{ "x": 0.695, "y": 0.914 }]}
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.072, "y": 0.475 },{ "x": 0.105, "y": 0.445 },{ "x": 0.212, "y": 0.914 },{ "x": 0.027, "y": 0.911 }]},
{"coordinates": [{ "x": 0.233, "y": 0.433 },{ "x": 0.268, "y": 0.409 },{ "x": 0.879, "y": 0.885 },{ "x": 0.695, "y": 0.914 }]}
])}
],
"0033_SanQuentin": [
{"name": "day", "path": "day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.064, "y": 0.311 },{ "x": 0.089, "y": 0.297 },{ "x": 0.210, "y": 0.410 },{ "x": 0.263, "y": 0.508 },{ "x": 0.344, "y": 0.863 },{ "x": 0.195, "y": 0.883 },{ "x": 0.177, "y": 0.533 },{ "x": 0.113, "y": 0.361 },{ "x": 0.051, "y": 0.299 }]},
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.064, "y": 0.311 },{ "x": 0.089, "y": 0.297 },{ "x": 0.210, "y": 0.410 },{ "x": 0.263, "y": 0.508 },{ "x": 0.344, "y": 0.863 },{ "x": 0.195, "y": 0.883 },{ "x": 0.177, "y": 0.533 },{ "x": 0.113, "y": 0.361 },{ "x": 0.051, "y": 0.299 }]},
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.064, "y": 0.311 },{ "x": 0.089, "y": 0.297 },{ "x": 0.210, "y": 0.410 },{ "x": 0.263, "y": 0.508 },{ "x": 0.344, "y": 0.863 },{ "x": 0.195, "y": 0.883 },{ "x": 0.177, "y": 0.533 },{ "x": 0.113, "y": 0.361 },{ "x": 0.051, "y": 0.299 }]},
])}
],
"0307_SanQuentin": [
{"name": "day", "path": "daytime.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.551, "y": 0.395 },{ "x": 0.568, "y": 0.391 },{ "x": 0.995, "y": 0.693 },{ "x": 0.996, "y": 0.794 }]},
{"coordinates": [{ "x": 0.448, "y": 0.379 },{ "x": 0.473, "y": 0.379 },{ "x": 0.661, "y": 0.908 },{ "x": 0.393, "y": 0.920 }]},
])},
{"name": "day", "path": "daytime2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.551, "y": 0.395 },{ "x": 0.568, "y": 0.391 },{ "x": 0.995, "y": 0.693 },{ "x": 0.996, "y": 0.794 }]},
{"coordinates": [{ "x": 0.448, "y": 0.379 },{ "x": 0.473, "y": 0.379 },{ "x": 0.661, "y": 0.908 },{ "x": 0.393, "y": 0.920 }]},
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.551, "y": 0.395 },{ "x": 0.568, "y": 0.391 },{ "x": 0.995, "y": 0.693 },{ "x": 0.996, "y": 0.794 }]},
{"coordinates": [{ "x": 0.448, "y": 0.379 },{ "x": 0.473, "y": 0.379 },{ "x": 0.661, "y": 0.908 },{ "x": 0.393, "y": 0.920 }]},
])},
{"name": "night", "path": "night2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.551, "y": 0.395 },{ "x": 0.568, "y": 0.391 },{ "x": 0.995, "y": 0.693 },{ "x": 0.996, "y": 0.794 }]},
{"coordinates": [{ "x": 0.448, "y": 0.379 },{ "x": 0.473, "y": 0.379 },{ "x": 0.661, "y": 0.908 },{ "x": 0.393, "y": 0.920 }]},
])}
],
"0308_SanQuentin": [
{"name": "day", "path": "daytime.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.713, "y": 0.327 },{ "x": 0.741, "y": 0.324 },{ "x": 0.709, "y": 0.914 },{ "x": 0.483, "y": 0.913 }]},
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.713, "y": 0.327 },{ "x": 0.741, "y": 0.324 },{ "x": 0.709, "y": 0.914 },{ "x": 0.483, "y": 0.913 }]},
])},
{"name": "foggy day (Vehicle on the side)", "path": "foggy_day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.713, "y": 0.327 },{ "x": 0.741, "y": 0.324 },{ "x": 0.709, "y": 0.914 },{ "x": 0.483, "y": 0.913 }]},
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.713, "y": 0.327 },{ "x": 0.741, "y": 0.324 },{ "x": 0.709, "y": 0.914 },{ "x": 0.483, "y": 0.913 }]},
])}
],
"0309_SanQuentin": [
{"name": "day", "path": "day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.678, "y": 0.411 },{ "x": 0.696, "y": 0.407 },{ "x": 0.718, "y": 0.915 },{ "x": 0.438, "y": 0.917 }]},
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.678, "y": 0.411 },{ "x": 0.696, "y": 0.407 },{ "x": 0.718, "y": 0.915 },{ "x": 0.438, "y": 0.917 }]},
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.678, "y": 0.411 },{ "x": 0.696, "y": 0.407 },{ "x": 0.718, "y": 0.915 },{ "x": 0.438, "y": 0.917 }]},
])}
],
"0310_SanQuentin": [
{"name": "day", "path": "daytime.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.682, "y": 0.458 },{ "x": 0.700, "y": 0.454 },{ "x": 0.729, "y": 0.907 },{ "x": 0.466, "y": 0.909 }]},
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.682, "y": 0.458 },{ "x": 0.700, "y": 0.454 },{ "x": 0.729, "y": 0.907 },{ "x": 0.466, "y": 0.909 }]},
])},
{"name": "night", "path": "night_2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.682, "y": 0.458 },{ "x": 0.700, "y": 0.454 },{ "x": 0.729, "y": 0.907 },{ "x": 0.466, "y": 0.909 }]},
])}
],
"0313_SanQuentin": [
{"name": "day", "path": "day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.656, "y": 0.283 },{ "x": 0.674, "y": 0.285 },{ "x": 0.675, "y": 0.913 },{ "x": 0.328, "y": 0.912 }]},
])},
{"name": "foggy day", "path": "foggy_day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.656, "y": 0.283 },{ "x": 0.674, "y": 0.285 },{ "x": 0.675, "y": 0.913 },{ "x": 0.328, "y": 0.912 }]},
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.656, "y": 0.283 },{ "x": 0.674, "y": 0.285 },{ "x": 0.675, "y": 0.913 },{ "x": 0.328, "y": 0.912 }]},
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.656, "y": 0.283 },{ "x": 0.674, "y": 0.285 },{ "x": 0.675, "y": 0.913 },{ "x": 0.328, "y": 0.912 }]},
])},
],
"0317_SanQuentin": [
{"name": "day (vehicle on the side)", "path": "daytime_4.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.539, "y": 0.461 },{ "x": 0.569, "y": 0.457 },{ "x": 0.578, "y": 0.913 },{ "x": 0.322, "y": 0.913 }]},
])},
{"name": "day (vehicle on the side)", "path": "daytime_5.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.539, "y": 0.461 },{ "x": 0.569, "y": 0.457 },{ "x": 0.578, "y": 0.913 },{ "x": 0.322, "y": 0.913 }]},
])},
{"name": "day (vehicle on the side)", "path": "daytime_2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.539, "y": 0.461 },{ "x": 0.569, "y": 0.457 },{ "x": 0.578, "y": 0.913 },{ "x": 0.322, "y": 0.913 }]},
])},
{"name": "evening (vehicle on the side)", "path": "evening_2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.539, "y": 0.461 },{ "x": 0.569, "y": 0.457 },{ "x": 0.578, "y": 0.913 },{ "x": 0.322, "y": 0.913 }]},
])},
{"name": "evening (vehicle on the side)", "path": "eve.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.539, "y": 0.461 },{ "x": 0.569, "y": 0.457 },{ "x": 0.578, "y": 0.913 },{ "x": 0.322, "y": 0.913 }]},
])},
{"name": "night (vehicle on the side)", "path": "night_2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.539, "y": 0.461 },{ "x": 0.569, "y": 0.457 },{ "x": 0.578, "y": 0.913 },{ "x": 0.322, "y": 0.913 }]},
])},
],
"0321_SanQuentin": [
{"name": "day", "path": "day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.647, "y": 0.297 },{ "x": 0.682, "y": 0.301 },{ "x": 0.704, "y": 0.903 },{ "x": 0.346, "y": 0.919 }]},
])},
{"name": "day", "path": "day2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.647, "y": 0.297 },{ "x": 0.682, "y": 0.301 },{ "x": 0.704, "y": 0.903 },{ "x": 0.346, "y": 0.919 }]},
])},
{"name": "night (vehicle on the side)", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.647, "y": 0.297 },{ "x": 0.682, "y": 0.301 },{ "x": 0.704, "y": 0.903 },{ "x": 0.346, "y": 0.919 }]},
])}
],
"0322_SanQuentin": [
{"name": "day", "path": "day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.672, "y": 0.421 },{ "x": 0.696, "y": 0.421 },{ "x": 0.660, "y": 0.914 },{ "x": 0.418, "y": 0.913 }]},
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.672, "y": 0.421 },{ "x": 0.696, "y": 0.421 },{ "x": 0.660, "y": 0.914 },{ "x": 0.418, "y": 0.913 }]},
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.672, "y": 0.421 },{ "x": 0.696, "y": 0.421 },{ "x": 0.660, "y": 0.914 },{ "x": 0.418, "y": 0.913 }]},
])},
],
"0327_SanQuentin": [
{"name": "day", "path": "day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.560, "y": 0.481 },{ "x": 0.619, "y": 0.486 },{ "x": 0.572, "y": 0.920 },{ "x": 0.433, "y": 0.916 }]},
{"coordinates": [{ "x": 0.357, "y": 0.266 },{ "x": 0.368, "y": 0.280 },{ "x": 0.002, "y": 0.611 },{ "x": 0.001, "y": 0.559 }]},
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.560, "y": 0.481 },{ "x": 0.619, "y": 0.486 },{ "x": 0.572, "y": 0.920 },{ "x": 0.433, "y": 0.916 }]},
{"coordinates": [{ "x": 0.357, "y": 0.266 },{ "x": 0.368, "y": 0.280 },{ "x": 0.002, "y": 0.611 },{ "x": 0.001, "y": 0.559 }]},
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.560, "y": 0.481 },{ "x": 0.619, "y": 0.486 },{ "x": 0.572, "y": 0.920 },{ "x": 0.433, "y": 0.916 }]},
{"coordinates": [{ "x": 0.357, "y": 0.266 },{ "x": 0.368, "y": 0.280 },{ "x": 0.002, "y": 0.611 },{ "x": 0.001, "y": 0.559 }]},
])},
],
"0468_Novato": [
{"name": "day", "path": "daytime.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.997, "y": 0.433 },{ "x": 0.929, "y": 0.481 },{ "x": 0.902, "y": 0.523 },{ "x": 0.906, "y": 0.566 },{ "x": 0.997, "y": 0.651 }]},
{"coordinates": [{ "x": 0.172, "y": 0.582 },{ "x": 0.273, "y": 0.587 },{ "x": 0.261, "y": 0.920 },{ "x": 0.097, "y": 0.918 }]},
])},
{"name": "day", "path": "daytime_2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.997, "y": 0.433 },{ "x": 0.929, "y": 0.481 },{ "x": 0.902, "y": 0.523 },{ "x": 0.906, "y": 0.566 },{ "x": 0.997, "y": 0.651 }]},
{"coordinates": [{ "x": 0.172, "y": 0.582 },{ "x": 0.273, "y": 0.587 },{ "x": 0.261, "y": 0.920 },{ "x": 0.097, "y": 0.918 }]},
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.997, "y": 0.433 },{ "x": 0.929, "y": 0.481 },{ "x": 0.902, "y": 0.523 },{ "x": 0.906, "y": 0.566 },{ "x": 0.997, "y": 0.651 }]},
{"coordinates": [{ "x": 0.172, "y": 0.582 },{ "x": 0.273, "y": 0.587 },{ "x": 0.261, "y": 0.920 },{ "x": 0.097, "y": 0.918 }]},
])},
{"name": "evening", "path": "evening2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.997, "y": 0.433 },{ "x": 0.929, "y": 0.481 },{ "x": 0.902, "y": 0.523 },{ "x": 0.906, "y": 0.566 },{ "x": 0.997, "y": 0.651 }]},
{"coordinates": [{ "x": 0.172, "y": 0.582 },{ "x": 0.273, "y": 0.587 },{ "x": 0.261, "y": 0.920 },{ "x": 0.097, "y": 0.918 }]},
])},
],
"0483_Petaluma": [
{"name": "day", "path": "day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.582, "y": 0.561 },{ "x": 0.592, "y": 0.563 },{ "x": 0.664, "y": 0.614 },{ "x": 0.773, "y": 0.744 },{ "x": 0.871, "y": 0.916 },{ "x": 0.791, "y": 0.923 }]},
])},
{"name": "evening", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.582, "y": 0.561 },{ "x": 0.592, "y": 0.563 },{ "x": 0.664, "y": 0.614 },{ "x": 0.773, "y": 0.744 },{ "x": 0.871, "y": 0.916 },{ "x": 0.791, "y": 0.923 }]},
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.582, "y": 0.561 },{ "x": 0.592, "y": 0.563 },{ "x": 0.664, "y": 0.614 },{ "x": 0.773, "y": 0.744 },{ "x": 0.871, "y": 0.916 },{ "x": 0.791, "y": 0.923 }]},
])},
],
"0548_SanRafael": [
{"name": "day", "path": "day2.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.709, "y": 0.347 },{ "x": 0.746, "y": 0.347 },{ "x": 0.695, "y": 0.912 },{ "x": 0.553, "y": 0.909 }]},
{"coordinates": [{ "x": 0.440, "y": 0.363 },{ "x": 0.456, "y": 0.371 },{ "x": 0.013, "y": 0.708 },{ "x": 0.002, "y": 0.668 }]},
])},
{"name": "foggy day", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.709, "y": 0.347 },{ "x": 0.746, "y": 0.347 },{ "x": 0.695, "y": 0.912 },{ "x": 0.553, "y": 0.909 }]},
{"coordinates": [{ "x": 0.440, "y": 0.363 },{ "x": 0.456, "y": 0.371 },{ "x": 0.013, "y": 0.708 },{ "x": 0.002, "y": 0.668 }]},
])}
],
"0566_Sausalito": [
{"name": "day", "path": "day.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.643, "y": 0.267 },{ "x": 0.707, "y": 0.289 },{ "x": 0.615, "y": 0.635 },{ "x": 0.448, "y": 0.669 }]},
])}
],
"0569_Novato": [
{"name": "day", "path": "evening.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.399, "y": 0.615 },{ "x": 0.307, "y": 0.643 },{ "x": 0.282, "y": 0.669 },{ "x": 0.300, "y": 0.712 },{ "x": 0.369, "y": 0.754 },{ "x": 0.551, "y": 0.840 },{ "x": 0.670, "y": 0.921 },{ "x": 0.815, "y": 0.918 },{ "x": 0.555, "y": 0.789 },{ "x": 0.415, "y": 0.721 },{ "x": 0.364, "y": 0.663 },{ "x": 0.422, "y": 0.621 }]},
])},
{"name": "night", "path": "night.jpg", "roi": json.dumps([
{"coordinates": [{ "x": 0.399, "y": 0.615 },{ "x": 0.307, "y": 0.643 },{ "x": 0.282, "y": 0.669 },{ "x": 0.300, "y": 0.712 },{ "x": 0.369, "y": 0.754 },{ "x": 0.551, "y": 0.840 },{ "x": 0.670, "y": 0.921 },{ "x": 0.815, "y": 0.918 },{ "x": 0.555, "y": 0.789 },{ "x": 0.415, "y": 0.721 },{ "x": 0.364, "y": 0.663 },{ "x": 0.422, "y": 0.621 }]},
])}
]
}
def load_model(model_name):
model_path = hf_hub_download(repo_id=f"Yolov7/{model_name}", filename=f"{model_name}.pt")
return model_path
model_names = ["yolov7", "yolov7-e6e", "yolov7-e6"]
models = {model_name: load_model(model_name) for model_name in model_names}
def parse_rois(roi_json):
"""
Parse the ROI JSON structure and return a list of ROI polygons.
"""
rois_data = json.loads(roi_json)
roi_polygons = []
for roi in rois_data:
coordinates = roi["coordinates"]
roi_points = [(int(coord["x"]), int(coord["y"])) for coord in coordinates]
roi_polygons.append(roi_points)
return roi_polygons
rois = []
names= [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
'hair drier', 'toothbrush' ]
desired_classes = ['person', 'bicycle', 'car', 'motorcycle', 'bus', 'train', 'truck']
desired_indices = [names.index(cls) for cls in desired_classes if cls in names]
def detect(img, model, rois):
if img is None:
raise ValueError("No image provided!")
img.save("Inference/test.jpg")
source = 'Inference/'
weights = models[model]
imgsz = 640
# Parse the ROIs from the textbox input
rois_data = json.loads(rois)
device = torch.device('cpu')
model = attempt_load(weights, map_location=device)
stride = int(model.stride.max())
imgsz = check_img_size(imgsz, s=stride)
dataset = LoadImages(source, img_size=imgsz, stride=stride)
names = model.module.names if hasattr(model, 'module') else model.names
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
for path, img, im0s, _ in dataset:
img = torch.from_numpy(img).to(device).float()
img /= 255.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
pred = model(img)[0]
pred = non_max_suppression(pred, 0.25, 0.45)
p, s, im0 = path, '', im0s
# Draw ROI polygons on the image
roi_points_list = []
for roi in rois_data:
roi_points = [(int(coord['x'] * im0.shape[1]), int(coord['y'] * im0.shape[0])) for coord in roi['coordinates']]
roi_points_list.append(roi_points)
# Initially set ROI color to red
cv2.polylines(im0, [np.array(roi_points)], isClosed=True, color=(0, 0, 255), thickness=2)
for i, det in enumerate(pred):
p, s, im0 = path, '', im0s
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]]
if len(det):
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
# Filter detections based on ROIs and desired classes
filtered_detections = []
for *xyxy, conf, cls in reversed(det):
if int(cls) not in desired_indices:
continue
x_center = float((xyxy[0] + xyxy[2]) / 2)
y_center = float((xyxy[1] + xyxy[3]) / 2)
inside_roi = False
for roi_points in roi_points_list:
if cv2.pointPolygonTest(np.array(roi_points, dtype=np.float32), (x_center, y_center), False) >= 0:
inside_roi = True
# Change ROI color to green if a car is detected inside
cv2.polylines(im0, [np.array(roi_points)], isClosed=True, color=(0, 255, 0), thickness=2)
break
filtered_detections.append((*xyxy, conf, cls, inside_roi))
# Plot the detections with the desired color
for *xyxy, conf, cls, inside_roi in filtered_detections:
# Set bbox color to blue
color = (255, 0, 0)
label = f'{names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, im0, label=label, color=color, line_thickness=1)
return Image.fromarray(im0[:,:,::-1])
def get_dropdown_choices():
choices = []
for camera, images in cameras.items():
for image in images:
choice = f"{camera} - {image['name']}"
choices.append(choice)
return choices
def detect_with_camera(choice):
# Extract the camera and image_name from the choice
camera, image_name = choice.split(" - ")
# Find the correct image and roi_json based on the camera and image_name
for image in cameras[camera]:
if image['name'] == image_name:
img_path = f"{camera}/{image['path']}"
roi_json = image['roi']
break
else:
raise ValueError(f"No image found for camera {camera} with name {image_name}")
# Load the image based on the camera and image_name
img = Image.open(img_path)
# Process the image with the detection model
processed_img = detect(img, "yolov7", roi_json)
return img, processed_img
iface = gr.Interface(
detect_with_camera,
gr.Dropdown(choices=get_dropdown_choices(), label="Select Camera and Image"),
[
gr.Image(type="pil", label="Raw Camera Stream"),
gr.Image(type="pil", label="Processed Stream")
],
live=True,
title="Vehicle Detection on Roadside",
description = ("Navigating through the intricate pathways of roadside vehicle detection, "
"the system identifies and analyzes vehicles using advanced detection algorithms.\n\n"
"Select a Camera and Image to Dive Into the Analysis")
)
iface.launch()