Spaces:
Runtime error
Runtime error
RyanPham19092002
commited on
Commit
·
6ceeb55
1
Parent(s):
bf7c448
Add application file
Browse files- app.py +111 -0
- requirements.txt +130 -0
- yolov5s.pt +3 -0
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
import requests
|
5 |
+
import json
|
6 |
+
import cv2
|
7 |
+
from PIL import Image
|
8 |
+
from timeit import default_timer as timer
|
9 |
+
import numpy as np
|
10 |
+
import aiofiles
|
11 |
+
from transformers import AutoModel
|
12 |
+
|
13 |
+
model = torch.hub.load('ultralytics/yolov5','yolov5s', pretrained=True)
|
14 |
+
#model1 = AutoModel.from_pretrained(model)
|
15 |
+
cnt = 0
|
16 |
+
def LCR(bbox,x_img, y_img):
|
17 |
+
x1 = bbox[0]/x_img
|
18 |
+
x2 = bbox[2]/x_img
|
19 |
+
if x1 < 0.2 and x2 < 0.2 :
|
20 |
+
location = "Left"
|
21 |
+
elif x1 > 0.8 and x2 > 0.8:
|
22 |
+
location = "Right"
|
23 |
+
elif x1 < 0.2 and (x2 <= 0.8 and x2 >= 0.2):
|
24 |
+
if (x1 + x2) < 0.4:
|
25 |
+
location = "Left"
|
26 |
+
else:
|
27 |
+
location = "Center"
|
28 |
+
elif x2 > 0.8 and (x1 <= 0.8 and x1 >= 0.2):
|
29 |
+
if (x1 + x2) > 1.6:
|
30 |
+
location = "Right"
|
31 |
+
else:
|
32 |
+
location = "Center"
|
33 |
+
else:
|
34 |
+
location = "Center"
|
35 |
+
print(f"x1 {x1} x2 {x2} bbox0 {bbox[0]} bbox2 {bbox[2]} x_img {x_img} LocationLCR {location}")
|
36 |
+
return location
|
37 |
+
|
38 |
+
def ACB(bbox, x_img, y_img, location):
|
39 |
+
y1 = bbox[1]/y_img
|
40 |
+
y2 = bbox[3]/y_img
|
41 |
+
if location == "Center":
|
42 |
+
if y1 < 0.33333 and y2 < 0.33333 :
|
43 |
+
location = "Above"
|
44 |
+
elif y1 > 0.66667 and y2 > 0.66667:
|
45 |
+
location = "Below"
|
46 |
+
elif y1 < 0.33333 and (y2 <= 0.66667 and y2 >= 0.33333):
|
47 |
+
if (y1 + y2) < 0.66667:
|
48 |
+
location = "Above"
|
49 |
+
else:
|
50 |
+
location = "Center"
|
51 |
+
elif y2 > 0.66667 and (y1 <= 0.66667 and y1 >= 0.33333):
|
52 |
+
if (y1 + y2) > 1.33333:
|
53 |
+
location = "Below"
|
54 |
+
else:
|
55 |
+
location = "Center"
|
56 |
+
else:
|
57 |
+
location = "Center"
|
58 |
+
else:
|
59 |
+
pass
|
60 |
+
print(f"y1 {y1} y2 {y2} bbox1 {bbox[1]} bbox3 {bbox[3]} y_img {y_img} Location{location}")
|
61 |
+
|
62 |
+
return location
|
63 |
+
#print(bbox[0])
|
64 |
+
|
65 |
+
def turn_img_into_fileJSON(frame):
|
66 |
+
start_time = timer()
|
67 |
+
x_img, y_img = frame.size
|
68 |
+
print(x_img,y_img)
|
69 |
+
global cnt
|
70 |
+
objects = []
|
71 |
+
|
72 |
+
prediction = model(frame)
|
73 |
+
for det in prediction.xyxy[0]:
|
74 |
+
class_id = int(det[5])
|
75 |
+
class_name = model.names[class_id]
|
76 |
+
confidence = float(det[4])
|
77 |
+
bbox = det[:4].tolist()
|
78 |
+
if(confidence >= 0.5):
|
79 |
+
location = LCR(bbox, x_img, y_img)
|
80 |
+
location = ACB(bbox, x_img, y_img, location)
|
81 |
+
# Save the results to the list
|
82 |
+
objects.append({
|
83 |
+
'Class': class_name,
|
84 |
+
#'BoundingBox': bbox,
|
85 |
+
'Location': location,
|
86 |
+
'Confidence': confidence
|
87 |
+
})
|
88 |
+
with open('{:05d}.json'.format(cnt) , 'w') as f:
|
89 |
+
json.dump(objects, f)
|
90 |
+
cnt += 1
|
91 |
+
pred_time = round(timer() - start_time, 5)
|
92 |
+
json_str = json.dumps(objects)
|
93 |
+
return json_str, pred_time
|
94 |
+
#path = [["D:/cuoc_thi/object-detection/download.jpg"],["C:/Users/ACER/Pictures/mydestiny/273536337_788402492117531_8798195010554693138_n.jpg"]]
|
95 |
+
title = "Object-detection"
|
96 |
+
description = "An EfficientNetB2 feature extractor computer vision model to classify images of object."
|
97 |
+
article = "Created by Ryan"
|
98 |
+
# Create the Gradio demo
|
99 |
+
demo = gr.Interface(fn=turn_img_into_fileJSON, # mapping function from input to output
|
100 |
+
inputs="pil", # what are the inputs?
|
101 |
+
outputs=[gr.JSON(label="JSON Output"),
|
102 |
+
#gr.Label(num_top_classes=80, label="Predictions"),
|
103 |
+
gr.Number(label="Prediction time (s)")],
|
104 |
+
#gr.outputs.Label(num_top_classes= 80),
|
105 |
+
#examples=path,
|
106 |
+
title=title,
|
107 |
+
description=description,
|
108 |
+
article=article,
|
109 |
+
live = True)
|
110 |
+
#demo.launch()
|
111 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pybind11==2.11.1
|
2 |
+
aiofiles==23.1.0
|
3 |
+
aiohttp==3.8.5
|
4 |
+
aiosignal==1.3.1
|
5 |
+
altair==5.0.1
|
6 |
+
annotated-types==0.5.0
|
7 |
+
anyio==3.7.1
|
8 |
+
async-timeout==4.0.2
|
9 |
+
attrs==23.1.0
|
10 |
+
charset-normalizer==3.2.0
|
11 |
+
click==8.1.6
|
12 |
+
colorama==0.4.6
|
13 |
+
contourpy==1.1.0
|
14 |
+
cycler==0.11.0
|
15 |
+
exceptiongroup==1.1.2
|
16 |
+
fastapi==0.100.1
|
17 |
+
ffmpeg==1.4
|
18 |
+
ffmpy==0.3.0
|
19 |
+
filelock==3.12.2
|
20 |
+
fonttools==4.41.1
|
21 |
+
frozenlist==1.4.0
|
22 |
+
fsspec==2023.6.0
|
23 |
+
glog==0.3
|
24 |
+
h11==0.14.0
|
25 |
+
h2==4.1.0
|
26 |
+
hpack==4.0.0
|
27 |
+
httpcore==0.17.3
|
28 |
+
httpx==0.24.1
|
29 |
+
huggingface_hub==0.16.4
|
30 |
+
hyperframe==6.0.1
|
31 |
+
idna==3.4
|
32 |
+
importlib-metadata==6.8.0
|
33 |
+
importlib-resources==6.0.0
|
34 |
+
importlib_metadata==6.8.0
|
35 |
+
importlib_resources==6.0.0
|
36 |
+
intel-openmp==2022.*
|
37 |
+
jinja2==3.1.2
|
38 |
+
joblib==1.3.0
|
39 |
+
jsonschema==4.18.4
|
40 |
+
jsonschema-specifications==2023.7.1
|
41 |
+
kiwisolver==1.4.4
|
42 |
+
libtiff==0.4.2
|
43 |
+
linkify-it-py==2.0.0
|
44 |
+
markdown-it-py==2.2.0
|
45 |
+
markupsafe==2.1.3
|
46 |
+
mdit-py-plugins==0.3.3
|
47 |
+
mdurl==0.1.0
|
48 |
+
mkl==2022.1.0
|
49 |
+
multidict==6.0.4
|
50 |
+
multiprocess==0.70.15
|
51 |
+
munkres==1.1.4
|
52 |
+
numpy==1.24.4
|
53 |
+
orc==0.1.0
|
54 |
+
orjson==3.9.2
|
55 |
+
packaging==23.1
|
56 |
+
pandas==2.0.3
|
57 |
+
pillow==10.0.0
|
58 |
+
pip==23.2.1
|
59 |
+
pkgutil-resolve-name==1.3.10
|
60 |
+
pyarrow==11.0.0
|
61 |
+
pydantic==2.1.1
|
62 |
+
pydantic-core==2.4.0
|
63 |
+
pydub==0.25.1
|
64 |
+
pyparsing==3.1.0
|
65 |
+
pysocks==1.7.1
|
66 |
+
python-dateutil==2.8.2
|
67 |
+
python-multipart==0.0.6
|
68 |
+
pytz==2023.3
|
69 |
+
pyyaml==6.0
|
70 |
+
re2==0.2.24
|
71 |
+
referencing==0.30.0
|
72 |
+
regex==2023.6.3
|
73 |
+
requests==2.31.0
|
74 |
+
rpds-py==0.9.2
|
75 |
+
sacremoses==0.0.53
|
76 |
+
safetensors==0.3.1
|
77 |
+
semantic_version==2.10.0
|
78 |
+
setuptools==68.0.0
|
79 |
+
six==1.16.0
|
80 |
+
snappy==3.1.1
|
81 |
+
sniffio==1.3.0
|
82 |
+
starlette==0.27.0
|
83 |
+
tbb==2021.10.0
|
84 |
+
tk==0.1.0
|
85 |
+
tokenizers==0.13.3
|
86 |
+
toolz==0.12.0
|
87 |
+
tqdm==4.65.0
|
88 |
+
transformers==4.31.0
|
89 |
+
typing-extensions==4.7.1
|
90 |
+
typing_extensions==4.7.1
|
91 |
+
uc-micro-py==1.0.1
|
92 |
+
unicodedata2==15.0.0
|
93 |
+
urllib3==2.0.4
|
94 |
+
uvicorn==0.23.1
|
95 |
+
vc==2018.7.10
|
96 |
+
websockets==11.0.3
|
97 |
+
wheel==0.41.0
|
98 |
+
win_inet_pton==1.1.0
|
99 |
+
xxhash==3.2.0
|
100 |
+
yarl==1.9.2
|
101 |
+
zipp==3.16.2
|
102 |
+
zstd==1.5.2
|
103 |
+
asttokens==2.2.1
|
104 |
+
backcall==0.2.0
|
105 |
+
decorator==5.1.1
|
106 |
+
executing==1.2.0
|
107 |
+
gitdb==4.0.10
|
108 |
+
gitpython==3.1.32
|
109 |
+
gradio==3.39.0
|
110 |
+
gradio-client==0.3.0
|
111 |
+
ipython==8.12.2
|
112 |
+
jedi==0.19.0
|
113 |
+
matplotlib-inline==0.1.6
|
114 |
+
mpmath==1.3.0
|
115 |
+
networkx==3.1
|
116 |
+
parso==0.8.3
|
117 |
+
pickleshare==0.7.5
|
118 |
+
prompt-toolkit==3.0.39
|
119 |
+
psutil==5.9.5
|
120 |
+
pure-eval==0.2.2
|
121 |
+
pygments==2.15.1
|
122 |
+
scipy==1.10.1
|
123 |
+
seaborn==0.12.2
|
124 |
+
smmap==5.0.0
|
125 |
+
stack-data==0.6.2
|
126 |
+
sympy==1.12
|
127 |
+
torch==2.0.1
|
128 |
+
torchvision==0.15.2
|
129 |
+
traitlets==5.9.0
|
130 |
+
wcwidth==0.2.6
|
yolov5s.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8b3b748c1e592ddd8868022e8732fde20025197328490623cc16c6f24d0782ee
|
3 |
+
size 14808437
|