Spaces:
Sleeping
Sleeping
Commit
•
52f506d
1
Parent(s):
aa00954
Update app.py
Browse files
app.py
CHANGED
@@ -77,6 +77,47 @@ def predict2(image_np):
|
|
77 |
|
78 |
return result_pil_img
|
79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
REPO_ID = "gregarific/23B718FMod"
|
82 |
detection_model = load_model()
|
@@ -86,7 +127,31 @@ detection_model = load_model()
|
|
86 |
# predicted_img = predict(image_arr)
|
87 |
# predicted_img.save('predicted.jpg')
|
88 |
|
89 |
-
gr.Interface(fn=predict,
|
|
|
|
|
|
|
|
|
|
|
90 |
inputs=gr.Image(type="pil"),
|
91 |
-
outputs=gr.Image(type="pil")
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
return result_pil_img
|
79 |
|
80 |
+
def predict_on_video(video_in_filepath, video_out_filepath, detection_model, category_index):
|
81 |
+
video_reader = cv2.VideoCapture(video_in_filepath)
|
82 |
+
frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
83 |
+
frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
|
84 |
+
fps = video_reader.get(cv2.CAP_PROP_FPS)
|
85 |
+
|
86 |
+
video_writer = cv2.VideoWriter(
|
87 |
+
video_out_filepath,
|
88 |
+
cv2.VideoWriter_fourcc(*'mp4v'),
|
89 |
+
fps,
|
90 |
+
(frame_w, frame_h)
|
91 |
+
)
|
92 |
+
while True:
|
93 |
+
ret, frame = video_reader.read()
|
94 |
+
if not ret:
|
95 |
+
break # Break the loop if the video is finished
|
96 |
+
|
97 |
+
processed_frame = predict(frame)
|
98 |
+
processed_frame_np = np.array(processed_frame)
|
99 |
+
video_writer.write(processed_frame_np)
|
100 |
+
|
101 |
+
# Release camera and close windows
|
102 |
+
video_reader.release()
|
103 |
+
video_writer.release()
|
104 |
+
cv2.destroyAllWindows()
|
105 |
+
cv2.waitKey(1)
|
106 |
+
video_reader.release()
|
107 |
+
video_writer.release()
|
108 |
+
cv2.destroyAllWindows()
|
109 |
+
cv2.waitKey(1)
|
110 |
+
|
111 |
+
# Function to process a video
|
112 |
+
def process_video(video_path):
|
113 |
+
output_path = "output_video.mp4" # Output path for the processed video
|
114 |
+
predict_on_video(video_path, output_path, detection_model, category_index)
|
115 |
+
return output_path
|
116 |
+
|
117 |
+
# Specify paths to example images
|
118 |
+
sample_images = [["sample1.jpg"], ["sample2.jpg"],
|
119 |
+
["sample3.jpg"]
|
120 |
+
]
|
121 |
|
122 |
REPO_ID = "gregarific/23B718FMod"
|
123 |
detection_model = load_model()
|
|
|
127 |
# predicted_img = predict(image_arr)
|
128 |
# predicted_img.save('predicted.jpg')
|
129 |
|
130 |
+
#gr.Interface(fn=predict,
|
131 |
+
# inputs=gr.Image(type="pil"),
|
132 |
+
# outputs=gr.Image(type="pil")
|
133 |
+
# ).launch(share=True)
|
134 |
+
|
135 |
+
tab1 = gr.Interface(fn=predict,
|
136 |
inputs=gr.Image(type="pil"),
|
137 |
+
outputs=gr.Image(type="pil"),
|
138 |
+
examples=sample_images,
|
139 |
+
title="Image - Object Detection (WheelChair vs Motorized WheelChair)",
|
140 |
+
description='Model used: SSD MobileNet V1 FPN 640x640.'
|
141 |
+
)
|
142 |
+
|
143 |
+
#gr.Interface(fn=predict,
|
144 |
+
# inputs=gr.Image(type="pil"),
|
145 |
+
# outputs=gr.Image(type="pil")
|
146 |
+
# ).launch(share=True)
|
147 |
+
tab2 = gr.Interface(
|
148 |
+
fn=process_video,
|
149 |
+
inputs=gr.File(label="Upload a video"),
|
150 |
+
outputs=gr.File(label="output"),
|
151 |
+
title='Video - Object Detection (WheelChair Type)'
|
152 |
+
)
|
153 |
+
|
154 |
+
|
155 |
+
iface = gr.TabbedInterface([tab1, tab2], tab_names = ['Image','Video'], title='WheelChair Type Detection')
|
156 |
+
|
157 |
+
iface.launch(share=True)
|