Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import cv2
|
4 |
+
|
5 |
+
from ultralytics import YOLO
|
6 |
+
|
7 |
+
"""
|
8 |
+
input_dir = '/home/student/PycharmProjects/pythonProjectYoloObjectPredict/test_images'
|
9 |
+
output_dir = '/home/student/PycharmProjects/pythonProjectYoloObjectPredict/'
|
10 |
+
|
11 |
+
image_files = [f for f in os.listdir(input_dir) if f.lower().endswith('.jpg')]
|
12 |
+
|
13 |
+
for image_file in image_files:
|
14 |
+
image_path = os.path.join(input_dir, image_file)
|
15 |
+
output_path = os.path.join(output_dir, f'{os.path.splitext(image_file)[0]}_result.jpg')
|
16 |
+
|
17 |
+
results = model.predict(image_path, conf=0.25, save=True, save_crop=True)
|
18 |
+
print(f'Predictions for {image_file}: {results}')
|
19 |
+
"""
|
20 |
+
|
21 |
+
# Provide the directory path where your images are located
|
22 |
+
image_directory = '/content/drive/MyDrive/Work/yolo/images/val'
|
23 |
+
|
24 |
+
jpg_files = [file for file in os.listdir(image_directory) if file.lower().endswith('.jpg')]
|
25 |
+
|
26 |
+
# Create a list of full paths to the JPG files
|
27 |
+
path = [os.path.join(image_directory, filename) for filename in jpg_files]
|
28 |
+
|
29 |
+
model = YOLO('/content/drive/MyDrive/Work/yolo/best.pt')
|
30 |
+
|
31 |
+
inputs_image = [
|
32 |
+
gr.components.Image(type="filepath", label="Input Image"),
|
33 |
+
]
|
34 |
+
outputs_image = [
|
35 |
+
gr.components.Image(type="numpy", label="Output Image"),
|
36 |
+
]
|
37 |
+
|
38 |
+
def show_preds_image(image_path):
|
39 |
+
image = cv2.imread(image_path)
|
40 |
+
outputs = model.predict(source=image_path)
|
41 |
+
results = outputs[0].cpu().numpy()
|
42 |
+
for i, det in enumerate(results.boxes.xyxy):
|
43 |
+
cv2.rectangle(
|
44 |
+
image,
|
45 |
+
(int(det[0]), int(det[1])),
|
46 |
+
(int(det[2]), int(det[3])),
|
47 |
+
color=(0, 0, 255),
|
48 |
+
thickness=2,
|
49 |
+
lineType=cv2.LINE_AA
|
50 |
+
)
|
51 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
52 |
+
|
53 |
+
interface_image = gr.Interface(
|
54 |
+
fn=show_preds_image,
|
55 |
+
inputs=inputs_image,
|
56 |
+
outputs=outputs_image,
|
57 |
+
title="Floor Plan Detector",
|
58 |
+
examples=path,
|
59 |
+
cache_examples=False,
|
60 |
+
)
|
61 |
+
|
62 |
+
gr.TabbedInterface(
|
63 |
+
[interface_image],
|
64 |
+
tab_names=['Image Inference'],
|
65 |
+
).queue().launch(debug=True)
|