Spaces:
Runtime error
Runtime error
bonosa
commited on
Commit
·
62146bf
1
Parent(s):
053cb21
app.py
CHANGED
@@ -16,22 +16,53 @@ def resize_image(image_path):
|
|
16 |
resized_img = cv2.resize(img, (512, 512), interpolation = cv2.INTER_LINEAR)
|
17 |
|
18 |
return resized_img
|
19 |
-
|
20 |
-
|
21 |
def prediction1(image_path):
|
22 |
-
#image
|
23 |
image = cv2.imread(image_path)
|
24 |
-
outputs = model.predict(
|
25 |
results = outputs[0].cpu().numpy()
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
for i, det in enumerate(results.boxes.xyxy):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
cv2.rectangle(
|
28 |
image,
|
29 |
(int(det[0]), int(det[1])),
|
30 |
(int(det[2]), int(det[3])),
|
31 |
-
color=(0,255, 0),
|
32 |
thickness=1,
|
33 |
lineType=cv2.LINE_AA,
|
34 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
36 |
|
37 |
inputs_image = [
|
@@ -47,7 +78,7 @@ interface_image = gr.Interface(
|
|
47 |
title="Pothole detection",
|
48 |
description="Detects potholes in images",
|
49 |
#cache_examples=True,
|
50 |
-
examples=
|
51 |
|
52 |
)
|
53 |
|
|
|
16 |
resized_img = cv2.resize(img, (512, 512), interpolation = cv2.INTER_LINEAR)
|
17 |
|
18 |
return resized_img
|
|
|
|
|
19 |
def prediction1(image_path):
|
20 |
+
# Read the image using OpenCV
|
21 |
image = cv2.imread(image_path)
|
22 |
+
outputs = model.predict(image_path)
|
23 |
results = outputs[0].cpu().numpy()
|
24 |
+
|
25 |
+
# Initialize maximum area and index
|
26 |
+
max_area = 0
|
27 |
+
max_index = -1
|
28 |
+
|
29 |
+
# Calculate areas and find the box with the maximum area
|
30 |
for i, det in enumerate(results.boxes.xyxy):
|
31 |
+
width = det[2] - det[0]
|
32 |
+
height = det[3] - det[1]
|
33 |
+
area = width * height
|
34 |
+
if area > max_area:
|
35 |
+
max_area = area
|
36 |
+
max_index = i
|
37 |
+
|
38 |
+
# Draw bounding box for each detected pothole
|
39 |
cv2.rectangle(
|
40 |
image,
|
41 |
(int(det[0]), int(det[1])),
|
42 |
(int(det[2]), int(det[3])),
|
43 |
+
color=(0, 255, 0),
|
44 |
thickness=1,
|
45 |
lineType=cv2.LINE_AA,
|
46 |
)
|
47 |
+
|
48 |
+
# Add label to the bounding box with the maximum area
|
49 |
+
if max_index != -1:
|
50 |
+
det = results.boxes.xyxy[max_index]
|
51 |
+
# Compute relative width and height
|
52 |
+
relative_width = (det[2] - det[0]) / image.shape[1]
|
53 |
+
relative_height = (det[3] - det[1]) / image.shape[0]
|
54 |
+
|
55 |
+
# Draw relative width and height on the bounding box
|
56 |
+
cv2.putText(
|
57 |
+
image,
|
58 |
+
f'W: {relative_width:.2f}, H: {relative_height:.2f}',
|
59 |
+
(int(det[0]), int(det[1]) - 5),
|
60 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
61 |
+
0.5,
|
62 |
+
(0, 0, 255),
|
63 |
+
1,
|
64 |
+
cv2.LINE_AA
|
65 |
+
)
|
66 |
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
67 |
|
68 |
inputs_image = [
|
|
|
78 |
title="Pothole detection",
|
79 |
description="Detects potholes in images",
|
80 |
#cache_examples=True,
|
81 |
+
examples=[['pothole1.jpg'], ['pothole2.jpg'], ['pothole3.jpg'],['pothole4.jpg']]
|
82 |
|
83 |
)
|
84 |
|