Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,25 @@ from ultralytics import YOLO
|
|
12 |
import numpy as np
|
13 |
import cv2
|
14 |
from modelscope.hub.snapshot_download import snapshot_download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Fix GLIBCXX dependency
|
17 |
os.environ['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH'
|
@@ -34,7 +53,7 @@ MODEL_NAME = 'iic/mPLUG-Owl3-7B-240728'
|
|
34 |
try:
|
35 |
model_dir = snapshot_download(MODEL_NAME,
|
36 |
cache_dir='./models',
|
37 |
-
revision='
|
38 |
except Exception as e:
|
39 |
raise RuntimeError(f"Model download failed: {str(e)}")
|
40 |
|
@@ -56,7 +75,7 @@ def is_video(filename):
|
|
56 |
|
57 |
@spaces.GPU
|
58 |
def load_model_and_tokenizer():
|
59 |
-
"""Load 8-bit quantized model
|
60 |
try:
|
61 |
torch.cuda.empty_cache()
|
62 |
gc.collect()
|
@@ -82,7 +101,7 @@ def load_model_and_tokenizer():
|
|
82 |
raise
|
83 |
|
84 |
def process_yolo_results(results):
|
85 |
-
"""Process YOLO detection results
|
86 |
machinery_mapping = {
|
87 |
'tower_crane': "Tower Crane",
|
88 |
'mobile_crane': "Mobile Crane",
|
@@ -103,29 +122,26 @@ def process_yolo_results(results):
|
|
103 |
|
104 |
counts = {"Worker": 0, **{v: 0 for v in machinery_mapping.values()}}
|
105 |
|
106 |
-
|
107 |
-
for
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
111 |
|
112 |
-
|
113 |
-
if
|
114 |
-
counts[
|
115 |
-
|
116 |
-
|
117 |
-
for key, value in machinery_mapping.items():
|
118 |
-
if key in cls_name:
|
119 |
-
counts[value] += 1
|
120 |
-
break
|
121 |
-
except Exception as e:
|
122 |
-
print(f"YOLO processing error: {str(e)}")
|
123 |
-
|
124 |
return counts["Worker"], sum(counts.values()) - counts["Worker"], counts
|
125 |
|
126 |
@spaces.GPU
|
127 |
def detect_people_and_machinery(media_path):
|
128 |
-
"""GPU-accelerated detection
|
129 |
try:
|
130 |
max_people = 0
|
131 |
max_machines = {k: 0 for k in [
|
@@ -169,7 +185,7 @@ def detect_people_and_machinery(media_path):
|
|
169 |
|
170 |
@spaces.GPU
|
171 |
def analyze_video_activities(video_path):
|
172 |
-
"""Video analysis with chunk processing
|
173 |
try:
|
174 |
model, tokenizer, processor = load_model_and_tokenizer()
|
175 |
responses = []
|
@@ -178,7 +194,6 @@ def analyze_video_activities(video_path):
|
|
178 |
frame_step = max(1, int(vr.get_avg_fps()))
|
179 |
total_frames = len(vr)
|
180 |
|
181 |
-
# Process in 16-frame chunks
|
182 |
for i in range(0, total_frames, 16):
|
183 |
end_idx = min(i+16, total_frames)
|
184 |
frames = [Image.fromarray(vr[j].asnumpy()) for j in range(i, end_idx)]
|
@@ -203,7 +218,7 @@ def analyze_video_activities(video_path):
|
|
203 |
|
204 |
@spaces.GPU
|
205 |
def analyze_image_activities(image_path):
|
206 |
-
"""Image analysis
|
207 |
try:
|
208 |
model, tokenizer, processor = load_model_and_tokenizer()
|
209 |
image = Image.open(image_path).convert("RGB")
|
@@ -225,7 +240,7 @@ def analyze_image_activities(image_path):
|
|
225 |
|
226 |
@spaces.GPU
|
227 |
def annotate_video_with_bboxes(video_path):
|
228 |
-
"""Video annotation with
|
229 |
try:
|
230 |
cap = cv2.VideoCapture(video_path)
|
231 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
@@ -241,7 +256,6 @@ def annotate_video_with_bboxes(video_path):
|
|
241 |
if not ret:
|
242 |
break
|
243 |
|
244 |
-
# Process every 5th frame to reduce load
|
245 |
if frame_count % 5 == 0:
|
246 |
results = YOLO_MODEL(frame)
|
247 |
counts = {}
|
@@ -275,7 +289,7 @@ def annotate_video_with_bboxes(video_path):
|
|
275 |
return None
|
276 |
|
277 |
def process_diary(day, date, media):
|
278 |
-
"""Main processing pipeline
|
279 |
try:
|
280 |
if not media:
|
281 |
return [day, date, "No data", "No data", "No data", "No data", None]
|
|
|
12 |
import numpy as np
|
13 |
import cv2
|
14 |
from modelscope.hub.snapshot_download import snapshot_download
|
15 |
+
from ultralytics.nn.modules import Conv, C2f
|
16 |
+
from torch import nn
|
17 |
+
import ultralytics.nn.modules as modules
|
18 |
+
|
19 |
+
# Add custom C3k2 module definition
|
20 |
+
class C3k2(nn.Module):
|
21 |
+
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5):
|
22 |
+
super().__init__()
|
23 |
+
c_ = int(c2 * e)
|
24 |
+
self.cv1 = Conv(c1, c_, 1, 1)
|
25 |
+
self.cv2 = Conv(c1, c_, 1, 1)
|
26 |
+
self.cv3 = Conv(2 * c_, c2, 1)
|
27 |
+
self.m = nn.Sequential(*(C2f(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
|
28 |
+
|
29 |
+
def forward(self, x):
|
30 |
+
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))
|
31 |
+
|
32 |
+
# Patch the Ultralytics module
|
33 |
+
modules.C3k2 = C3k2
|
34 |
|
35 |
# Fix GLIBCXX dependency
|
36 |
os.environ['LD_LIBRARY_PATH'] = '/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH'
|
|
|
53 |
try:
|
54 |
model_dir = snapshot_download(MODEL_NAME,
|
55 |
cache_dir='./models',
|
56 |
+
revision='main')
|
57 |
except Exception as e:
|
58 |
raise RuntimeError(f"Model download failed: {str(e)}")
|
59 |
|
|
|
75 |
|
76 |
@spaces.GPU
|
77 |
def load_model_and_tokenizer():
|
78 |
+
"""Load 8-bit quantized model"""
|
79 |
try:
|
80 |
torch.cuda.empty_cache()
|
81 |
gc.collect()
|
|
|
101 |
raise
|
102 |
|
103 |
def process_yolo_results(results):
|
104 |
+
"""Process YOLO detection results"""
|
105 |
machinery_mapping = {
|
106 |
'tower_crane': "Tower Crane",
|
107 |
'mobile_crane': "Mobile Crane",
|
|
|
122 |
|
123 |
counts = {"Worker": 0, **{v: 0 for v in machinery_mapping.values()}}
|
124 |
|
125 |
+
for r in results:
|
126 |
+
for box in r.boxes:
|
127 |
+
if box.conf.item() < 0.5:
|
128 |
+
continue
|
129 |
+
|
130 |
+
cls_name = YOLO_MODEL.names[int(box.cls.item())].lower()
|
131 |
+
if cls_name == 'worker':
|
132 |
+
counts["Worker"] += 1
|
133 |
+
continue
|
134 |
|
135 |
+
for key, value in machinery_mapping.items():
|
136 |
+
if key in cls_name:
|
137 |
+
counts[value] += 1
|
138 |
+
break
|
139 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
return counts["Worker"], sum(counts.values()) - counts["Worker"], counts
|
141 |
|
142 |
@spaces.GPU
|
143 |
def detect_people_and_machinery(media_path):
|
144 |
+
"""GPU-accelerated detection"""
|
145 |
try:
|
146 |
max_people = 0
|
147 |
max_machines = {k: 0 for k in [
|
|
|
185 |
|
186 |
@spaces.GPU
|
187 |
def analyze_video_activities(video_path):
|
188 |
+
"""Video analysis with chunk processing"""
|
189 |
try:
|
190 |
model, tokenizer, processor = load_model_and_tokenizer()
|
191 |
responses = []
|
|
|
194 |
frame_step = max(1, int(vr.get_avg_fps()))
|
195 |
total_frames = len(vr)
|
196 |
|
|
|
197 |
for i in range(0, total_frames, 16):
|
198 |
end_idx = min(i+16, total_frames)
|
199 |
frames = [Image.fromarray(vr[j].asnumpy()) for j in range(i, end_idx)]
|
|
|
218 |
|
219 |
@spaces.GPU
|
220 |
def analyze_image_activities(image_path):
|
221 |
+
"""Image analysis pipeline"""
|
222 |
try:
|
223 |
model, tokenizer, processor = load_model_and_tokenizer()
|
224 |
image = Image.open(image_path).convert("RGB")
|
|
|
240 |
|
241 |
@spaces.GPU
|
242 |
def annotate_video_with_bboxes(video_path):
|
243 |
+
"""Video annotation with detection overlay"""
|
244 |
try:
|
245 |
cap = cv2.VideoCapture(video_path)
|
246 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
256 |
if not ret:
|
257 |
break
|
258 |
|
|
|
259 |
if frame_count % 5 == 0:
|
260 |
results = YOLO_MODEL(frame)
|
261 |
counts = {}
|
|
|
289 |
return None
|
290 |
|
291 |
def process_diary(day, date, media):
|
292 |
+
"""Main processing pipeline"""
|
293 |
try:
|
294 |
if not media:
|
295 |
return [day, date, "No data", "No data", "No data", "No data", None]
|