Spaces:
Build error
Build error
Update services/detection_service.py
Browse files
services/detection_service.py
CHANGED
|
@@ -9,9 +9,15 @@ class DetectionService:
|
|
| 9 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
self.model.to(self.device)
|
| 11 |
self.model.eval()
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def detect_objects(self, image, confidence_threshold=0.9):
|
| 14 |
-
"""Detect objects in an image."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
inputs = self.processor(images=image, return_tensors="pt").to(self.device)
|
| 16 |
with torch.no_grad():
|
| 17 |
outputs = self.model(**inputs)
|
|
|
|
| 9 |
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
self.model.to(self.device)
|
| 11 |
self.model.eval()
|
| 12 |
+
self.frame_counter = 0
|
| 13 |
+
self.frame_skip = 5 # Process every 5th frame for performance
|
| 14 |
|
| 15 |
def detect_objects(self, image, confidence_threshold=0.9):
|
| 16 |
+
"""Detect objects in an image, skipping frames for performance."""
|
| 17 |
+
self.frame_counter += 1
|
| 18 |
+
if self.frame_counter % self.frame_skip != 0:
|
| 19 |
+
return [] # Skip detection for this frame
|
| 20 |
+
|
| 21 |
inputs = self.processor(images=image, return_tensors="pt").to(self.device)
|
| 22 |
with torch.no_grad():
|
| 23 |
outputs = self.model(**inputs)
|