Initial commit
Browse files- .gitattributes +3 -0
- __pycache__/app.cpython-311.pyc +0 -0
- app.py +56 -38
- dataset/images/train/input_image.jpg +0 -0
- out/fixed_folder/input_image_upscaled.jpg +0 -0
- traced_model.pt +2 -2
.gitattributes
CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
/home/myominhtet/Desktop/Group filter=lfs diff=lfs merge=lfs -text
|
37 |
+
Scraper/detect/yolo/yolov7-main/runs/train/best.pt filter=lfs diff=lfs merge=lfs -text
|
38 |
+
Scraper/detect/yolo/yolov7-main/runs/train/last.pt filter=lfs diff=lfs merge=lfs -text
|
__pycache__/app.cpython-311.pyc
ADDED
Binary file (4.24 kB). View file
|
|
app.py
CHANGED
@@ -1,61 +1,79 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
3 |
import subprocess
|
4 |
-
import
|
5 |
-
import
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
20 |
|
|
|
|
|
21 |
# Run the detection command
|
22 |
command = [
|
23 |
'python', 'yolo/yolov7-main/detect.py',
|
24 |
'--weights', weights_path,
|
25 |
'--conf-thres', str(conf),
|
26 |
'--img-size', str(img_size),
|
27 |
-
'--source',
|
28 |
'--project', 'out/', # Output directory
|
29 |
'--name', 'fixed_folder', # Folder name for results
|
30 |
'--exist-ok' # Don't increment folder name
|
31 |
]
|
32 |
|
33 |
-
# Execute the command
|
34 |
-
subprocess.run(command)
|
35 |
-
|
36 |
-
|
37 |
-
output_image_path = 'out/fixed_folder/input_image_upscaled.jpg'
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
|
46 |
-
|
47 |
-
output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
|
48 |
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
)
|
59 |
|
60 |
-
#
|
61 |
-
|
|
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
4 |
+
from fastapi.responses import FileResponse, PlainTextResponse
|
5 |
+
import shutil
|
6 |
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Define paths and parameters
|
10 |
+
weights_path = 'yolo/yolov7-main/runs/train/best.pt'
|
11 |
+
img_size = 640
|
12 |
+
conf = 0.20
|
13 |
+
source_folder = 'dataset/images/train/'
|
14 |
+
output_folder = 'out/fixed_folder/'
|
15 |
+
|
16 |
+
# Ensure folders exist
|
17 |
+
os.makedirs(source_folder, exist_ok=True)
|
18 |
+
os.makedirs(output_folder, exist_ok=True)
|
19 |
|
20 |
+
# Root endpoint to prevent 404 on /
|
21 |
+
@app.get("/")
|
22 |
+
async def root():
|
23 |
+
return PlainTextResponse("Welcome to the YOLOv7 Object Detection API. Use the /detect endpoint to upload an image.")
|
24 |
|
25 |
+
# Optional: Add a route for favicon.ico to avoid 404
|
26 |
+
@app.get("/favicon.ico", include_in_schema=False)
|
27 |
+
async def favicon():
|
28 |
+
return PlainTextResponse("", status_code=204)
|
29 |
|
30 |
+
# Define the detect function
|
31 |
+
def detect_and_crop(image_path: str):
|
32 |
# Run the detection command
|
33 |
command = [
|
34 |
'python', 'yolo/yolov7-main/detect.py',
|
35 |
'--weights', weights_path,
|
36 |
'--conf-thres', str(conf),
|
37 |
'--img-size', str(img_size),
|
38 |
+
'--source', image_path,
|
39 |
'--project', 'out/', # Output directory
|
40 |
'--name', 'fixed_folder', # Folder name for results
|
41 |
'--exist-ok' # Don't increment folder name
|
42 |
]
|
43 |
|
44 |
+
# Execute the command and check for errors
|
45 |
+
result = subprocess.run(command, capture_output=True, text=True)
|
46 |
+
if result.returncode != 0:
|
47 |
+
raise HTTPException(status_code=500, detail=f"Detection failed: {result.stderr}")
|
|
|
48 |
|
49 |
+
# Locate the output image in the expected output directory
|
50 |
+
output_files = os.listdir(output_folder)
|
51 |
+
output_image_path = None
|
52 |
+
for file_name in output_files:
|
53 |
+
if file_name.endswith(".jpg") or file_name.endswith(".jpeg") or file_name.endswith(".png"):
|
54 |
+
output_image_path = os.path.join(output_folder, file_name)
|
55 |
+
break
|
56 |
|
57 |
+
if not output_image_path or not os.path.exists(output_image_path):
|
58 |
+
raise HTTPException(status_code=404, detail="Output image not found.")
|
59 |
|
60 |
+
return output_image_path
|
|
|
61 |
|
62 |
+
# FastAPI endpoint to accept an image, perform detection, and return the processed image
|
63 |
+
@app.post("/detect")
|
64 |
+
async def detect_endpoint(file: UploadFile = File(...)):
|
65 |
+
# Save the uploaded file to the source folder
|
66 |
+
input_image_path = os.path.join(source_folder, 'input_image.jpg')
|
67 |
+
with open(input_image_path, "wb") as buffer:
|
68 |
+
shutil.copyfileobj(file.file, buffer)
|
69 |
|
70 |
+
# Perform detection and get the path to the output image
|
71 |
+
try:
|
72 |
+
output_image_path = detect_and_crop(input_image_path)
|
73 |
+
except HTTPException as e:
|
74 |
+
raise e
|
75 |
+
except Exception as e:
|
76 |
+
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
|
|
|
77 |
|
78 |
+
# Return the output image as a response
|
79 |
+
return FileResponse(output_image_path, media_type="image/jpeg")
|
dataset/images/train/input_image.jpg
CHANGED
![]() |
![]() |
out/fixed_folder/input_image_upscaled.jpg
CHANGED
![]() |
![]() |
traced_model.pt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2e3e93c89ce09bb5995f3d6d1446814239bdc677684bce26f3573e0e39591ae8
|
3 |
+
size 283665354
|