Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,8 @@ from transformers import Owlv2Processor, Owlv2ForObjectDetection
|
|
| 6 |
import numpy as np
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16")
|
| 12 |
model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-base-patch16").to(device)
|
|
@@ -32,7 +33,7 @@ def detect_objects_in_frame(image, target):
|
|
| 32 |
boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]
|
| 33 |
|
| 34 |
for box, score, label in zip(boxes, scores, labels):
|
| 35 |
-
if score.item() >= 0.
|
| 36 |
box = [round(i, 2) for i in box.tolist()]
|
| 37 |
object_label = text[label]
|
| 38 |
confidence = round(score.item(), 3)
|
|
@@ -58,10 +59,11 @@ def process_video(video_path, target, progress=gr.Progress()):
|
|
| 58 |
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 59 |
original_fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 60 |
original_duration = frame_count / original_fps
|
|
|
|
| 61 |
|
| 62 |
output_path = "output_video.mp4"
|
| 63 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 64 |
-
out = cv2.VideoWriter(output_path, fourcc,
|
| 65 |
|
| 66 |
batch_size = 64
|
| 67 |
frames = []
|
|
@@ -71,6 +73,9 @@ def process_video(video_path, target, progress=gr.Progress()):
|
|
| 71 |
if not ret:
|
| 72 |
break
|
| 73 |
|
|
|
|
|
|
|
|
|
|
| 74 |
pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 75 |
frames.append(pil_img)
|
| 76 |
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
# Check if CUDA is available, otherwise use CPU
|
| 10 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 11 |
|
| 12 |
processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16")
|
| 13 |
model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-base-patch16").to(device)
|
|
|
|
| 33 |
boxes, scores, labels = results[i]["boxes"], results[i]["scores"], results[i]["labels"]
|
| 34 |
|
| 35 |
for box, score, label in zip(boxes, scores, labels):
|
| 36 |
+
if score.item() >= 0.25:
|
| 37 |
box = [round(i, 2) for i in box.tolist()]
|
| 38 |
object_label = text[label]
|
| 39 |
confidence = round(score.item(), 3)
|
|
|
|
| 59 |
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 60 |
original_fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 61 |
original_duration = frame_count / original_fps
|
| 62 |
+
output_fps = 5
|
| 63 |
|
| 64 |
output_path = "output_video.mp4"
|
| 65 |
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 66 |
+
out = cv2.VideoWriter(output_path, fourcc, output_fps, (int(cap.get(3)), int(cap.get(4))))
|
| 67 |
|
| 68 |
batch_size = 64
|
| 69 |
frames = []
|
|
|
|
| 73 |
if not ret:
|
| 74 |
break
|
| 75 |
|
| 76 |
+
if frame % (original_fps // output_fps) != 0:
|
| 77 |
+
continue
|
| 78 |
+
|
| 79 |
pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 80 |
frames.append(pil_img)
|
| 81 |
|