Wang commited on
Commit
4e2d045
1 Parent(s): 232ff24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py CHANGED
@@ -83,6 +83,40 @@ def predict2(image_np):
83
 
84
  return result_pil_img
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  REPO_ID = "Louisw3399/burgerorfriesdetector"
88
  detection_model = load_model()
@@ -96,3 +130,10 @@ gr.Interface(fn=predict,
96
  inputs=gr.Image(type="pil"),
97
  outputs=gr.Image(type="pil")
98
  ).launch(share=True)
 
 
 
 
 
 
 
 
83
 
84
  return result_pil_img
85
 
86
+ import cv2
87
+
88
+ def predict_video(video_path):
89
+ cap = cv2.VideoCapture(video_path)
90
+ frame_width = int(cap.get(3))
91
+ frame_height = int(cap.get(4))
92
+
93
+ # Define the codec and create a video writer object
94
+ out = cv2.VideoWriter('output.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width, frame_height))
95
+
96
+ while cap.isOpened():
97
+ ret, frame = cap.read()
98
+ if not ret:
99
+ break
100
+
101
+ # Convert the frame to PIL image
102
+ pil_image = Image.fromarray(frame)
103
+
104
+ # Perform object detection on the frame
105
+ result_pil_img = predict(pil_image)
106
+
107
+ # Convert the result back to a NumPy array
108
+ result_np_img = tf.keras.utils.img_to_array(result_pil_img)
109
+
110
+ # Write the frame with detected objects to the video output
111
+ out.write(result_np_img.astype('uint8'))
112
+
113
+ # Release the video capture and writer objects
114
+ cap.release()
115
+ out.release()
116
+
117
+ return "output.avi"
118
+
119
+
120
 
121
  REPO_ID = "Louisw3399/burgerorfriesdetector"
122
  detection_model = load_model()
 
130
  inputs=gr.Image(type="pil"),
131
  outputs=gr.Image(type="pil")
132
  ).launch(share=True)
133
+
134
+ gr.Interface(
135
+ fn=predict_video,
136
+ inputs=gr.Video(type="file", label="Upload a video"),
137
+ outputs=gr.Video(type="file", label="Download the processed video")
138
+ ).launch(share=True)
139
+