Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,12 +20,14 @@ def initialize_gpu():
|
|
| 20 |
torch.randn(10).cuda()
|
| 21 |
initialize_gpu()
|
| 22 |
|
| 23 |
-
# Load YOLO model
|
| 24 |
-
YOLO_MODEL = YOLO('best_yolov11.pt')
|
| 25 |
|
| 26 |
-
# Model configuration
|
| 27 |
MODEL_NAME = 'iic/mPLUG-Owl3-7B-240728'
|
| 28 |
-
model_dir = snapshot_download(MODEL_NAME,
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Device setup
|
| 31 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -45,7 +47,7 @@ def is_video(filename):
|
|
| 45 |
|
| 46 |
@spaces.GPU
|
| 47 |
def load_model_and_tokenizer():
|
| 48 |
-
"""Load 4-bit quantized model
|
| 49 |
try:
|
| 50 |
torch.cuda.empty_cache()
|
| 51 |
gc.collect()
|
|
@@ -70,184 +72,10 @@ def load_model_and_tokenizer():
|
|
| 70 |
print(f"Model loading error: {str(e)}")
|
| 71 |
raise
|
| 72 |
|
| 73 |
-
|
| 74 |
-
"""Process YOLO detection results"""
|
| 75 |
-
machinery_mapping = {
|
| 76 |
-
'tower_crane': "Tower Crane",
|
| 77 |
-
'mobile_crane': "Mobile Crane",
|
| 78 |
-
'compactor': "Compactor/Roller",
|
| 79 |
-
'roller': "Compactor/Roller",
|
| 80 |
-
'bulldozer': "Bulldozer",
|
| 81 |
-
'dozer': "Bulldozer",
|
| 82 |
-
'excavator': "Excavator",
|
| 83 |
-
'dump_truck': "Dump Truck",
|
| 84 |
-
'truck': "Dump Truck",
|
| 85 |
-
'concrete_mixer_truck': "Concrete Mixer",
|
| 86 |
-
'loader': "Loader",
|
| 87 |
-
'pump_truck': "Pump Truck",
|
| 88 |
-
'pile_driver': "Pile Driver",
|
| 89 |
-
'grader': "Grader",
|
| 90 |
-
'other_vehicle': "Other Vehicle"
|
| 91 |
-
}
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
for r in results:
|
| 96 |
-
for box in r.boxes:
|
| 97 |
-
if box.conf.item() < 0.5:
|
| 98 |
-
continue
|
| 99 |
-
|
| 100 |
-
cls_name = YOLO_MODEL.names[int(box.cls.item())].lower()
|
| 101 |
-
if cls_name == 'worker':
|
| 102 |
-
counts["Worker"] += 1
|
| 103 |
-
continue
|
| 104 |
-
|
| 105 |
-
for key, value in machinery_mapping.items():
|
| 106 |
-
if key in cls_name:
|
| 107 |
-
counts[value] += 1
|
| 108 |
-
break
|
| 109 |
-
|
| 110 |
-
return counts["Worker"], sum(counts.values()) - counts["Worker"], counts
|
| 111 |
-
|
| 112 |
-
@spaces.GPU
|
| 113 |
-
def detect_people_and_machinery(media_path):
|
| 114 |
-
"""GPU-accelerated detection"""
|
| 115 |
-
try:
|
| 116 |
-
max_people = 0
|
| 117 |
-
max_machines = {k: 0 for k in [
|
| 118 |
-
"Tower Crane", "Mobile Crane", "Compactor/Roller", "Bulldozer",
|
| 119 |
-
"Excavator", "Dump Truck", "Concrete Mixer", "Loader",
|
| 120 |
-
"Pump Truck", "Pile Driver", "Grader", "Other Vehicle"
|
| 121 |
-
]}
|
| 122 |
-
|
| 123 |
-
if isinstance(media_path, str) and is_video(media_path):
|
| 124 |
-
cap = cv2.VideoCapture(media_path)
|
| 125 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 126 |
-
sample_rate = max(1, int(fps))
|
| 127 |
-
|
| 128 |
-
while cap.isOpened():
|
| 129 |
-
ret, frame = cap.read()
|
| 130 |
-
if not ret:
|
| 131 |
-
break
|
| 132 |
-
|
| 133 |
-
results = YOLO_MODEL(frame)
|
| 134 |
-
people, machines, types = process_yolo_results(results)
|
| 135 |
-
|
| 136 |
-
max_people = max(max_people, people)
|
| 137 |
-
for k in max_machines:
|
| 138 |
-
max_machines[k] = max(max_machines[k], types.get(k, 0))
|
| 139 |
-
|
| 140 |
-
cap.release()
|
| 141 |
-
else:
|
| 142 |
-
img = cv2.imread(media_path) if isinstance(media_path, str) else cv2.cvtColor(np.array(media_path), cv2.COLOR_RGB2BGR)
|
| 143 |
-
results = YOLO_MODEL(img)
|
| 144 |
-
max_people, _, types = process_yolo_results(results)
|
| 145 |
-
for k in max_machines:
|
| 146 |
-
max_machines[k] = types.get(k, 0)
|
| 147 |
-
|
| 148 |
-
filtered = {k: v for k, v in max_machines.items() if v > 0}
|
| 149 |
-
return max_people, sum(filtered.values()), filtered
|
| 150 |
-
|
| 151 |
-
except Exception as e:
|
| 152 |
-
print(f"Detection error: {str(e)}")
|
| 153 |
-
return 0, 0, {}
|
| 154 |
-
|
| 155 |
-
@spaces.GPU
|
| 156 |
-
def analyze_video_activities(video_path):
|
| 157 |
-
"""Video analysis with chunk processing"""
|
| 158 |
-
try:
|
| 159 |
-
model, tokenizer, processor = load_model_and_tokenizer()
|
| 160 |
-
responses = []
|
| 161 |
-
|
| 162 |
-
vr = VideoReader(video_path, ctx=cpu(0))
|
| 163 |
-
frame_step = max(1, int(vr.get_avg_fps()))
|
| 164 |
-
frames = [Image.fromarray(f.asnumpy()) for f in vr[::frame_step]]
|
| 165 |
-
|
| 166 |
-
# Process in chunks
|
| 167 |
-
for i in range(0, len(frames), 16):
|
| 168 |
-
chunk = frames[i:i+16]
|
| 169 |
-
inputs = processor(
|
| 170 |
-
[{"role": "user", "content": "Analyze construction activities", "video_frames": chunk}],
|
| 171 |
-
videos=[chunk]
|
| 172 |
-
).to(DEVICE)
|
| 173 |
-
|
| 174 |
-
response = model.generate(**inputs, max_new_tokens=200)
|
| 175 |
-
responses.append(response[0])
|
| 176 |
-
|
| 177 |
-
del model, tokenizer, processor
|
| 178 |
-
torch.cuda.empty_cache()
|
| 179 |
-
return "\n".join(responses)
|
| 180 |
-
|
| 181 |
-
except Exception as e:
|
| 182 |
-
print(f"Video analysis error: {str(e)}")
|
| 183 |
-
return "Activity analysis unavailable"
|
| 184 |
-
|
| 185 |
-
@spaces.GPU
|
| 186 |
-
def analyze_image_activities(image_path):
|
| 187 |
-
"""Image analysis pipeline"""
|
| 188 |
-
try:
|
| 189 |
-
model, tokenizer, processor = load_model_and_tokenizer()
|
| 190 |
-
image = Image.open(image_path).convert("RGB")
|
| 191 |
-
|
| 192 |
-
inputs = processor(
|
| 193 |
-
[{"role": "user", "content": "Analyze construction site", "images": [image]}],
|
| 194 |
-
images=[image]
|
| 195 |
-
).to(DEVICE)
|
| 196 |
-
|
| 197 |
-
response = model.generate(**inputs, max_new_tokens=200)
|
| 198 |
-
del model, tokenizer, processor
|
| 199 |
-
return response[0]
|
| 200 |
-
|
| 201 |
-
except Exception as e:
|
| 202 |
-
print(f"Image analysis error: {str(e)}")
|
| 203 |
-
return "Activity analysis unavailable"
|
| 204 |
-
|
| 205 |
-
@spaces.GPU
|
| 206 |
-
def annotate_video_with_bboxes(video_path):
|
| 207 |
-
"""Video annotation with real-time detection"""
|
| 208 |
-
cap = cv2.VideoCapture(video_path)
|
| 209 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 210 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 211 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 212 |
-
|
| 213 |
-
temp_file = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 214 |
-
writer = cv2.VideoWriter(temp_file.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 215 |
-
|
| 216 |
-
while cap.isOpened():
|
| 217 |
-
ret, frame = cap.read()
|
| 218 |
-
if not ret:
|
| 219 |
-
break
|
| 220 |
-
|
| 221 |
-
results = YOLO_MODEL(frame)
|
| 222 |
-
counts = {}
|
| 223 |
-
|
| 224 |
-
for r in results:
|
| 225 |
-
for box in r.boxes:
|
| 226 |
-
if box.conf.item() < 0.5:
|
| 227 |
-
continue
|
| 228 |
-
|
| 229 |
-
cls_id = int(box.cls.item())
|
| 230 |
-
class_name = YOLO_MODEL.names[cls_id]
|
| 231 |
-
counts[class_name] = counts.get(class_name, 0) + 1
|
| 232 |
-
|
| 233 |
-
# Draw bounding box
|
| 234 |
-
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
| 235 |
-
cv2.rectangle(frame, (x1, y1), (x2, y2), (0,255,0), 2)
|
| 236 |
-
cv2.putText(frame, f"{class_name} {box.conf.item():.2f}",
|
| 237 |
-
(x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 1)
|
| 238 |
-
|
| 239 |
-
# Add summary text
|
| 240 |
-
summary = ", ".join([f"{k}:{v}" for k,v in counts.items()])
|
| 241 |
-
cv2.putText(frame, summary, (10,30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255), 2)
|
| 242 |
-
|
| 243 |
-
writer.write(frame)
|
| 244 |
-
|
| 245 |
-
cap.release()
|
| 246 |
-
writer.release()
|
| 247 |
-
return temp_file.name
|
| 248 |
-
|
| 249 |
-
def process_diary(day, date, people, machinery, machinery_types, activities, media):
|
| 250 |
-
"""Main processing pipeline"""
|
| 251 |
try:
|
| 252 |
if not media:
|
| 253 |
return [day, date, "No data", "No data", "No data", "No data", None]
|
|
@@ -302,11 +130,19 @@ with gr.Blocks(title="Digital Site Diary", css="video {height: auto !important;}
|
|
| 302 |
model_activities = gr.Textbox(label="Activity Analysis", lines=4)
|
| 303 |
model_video = gr.Video(label="Safety Annotations")
|
| 304 |
|
|
|
|
| 305 |
submit_btn.click(
|
| 306 |
process_diary,
|
| 307 |
-
inputs=[day, date,
|
| 308 |
-
outputs=[
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
)
|
| 311 |
|
| 312 |
if __name__ == "__main__":
|
|
|
|
| 20 |
torch.randn(10).cuda()
|
| 21 |
initialize_gpu()
|
| 22 |
|
| 23 |
+
# Load YOLO model with relative path
|
| 24 |
+
YOLO_MODEL = YOLO('best_yolov11.pt')
|
| 25 |
|
| 26 |
+
# Model configuration with quantization
|
| 27 |
MODEL_NAME = 'iic/mPLUG-Owl3-7B-240728'
|
| 28 |
+
model_dir = snapshot_download(MODEL_NAME,
|
| 29 |
+
revision='v1.0.0', # Specific revision
|
| 30 |
+
cache_dir='./models')
|
| 31 |
|
| 32 |
# Device setup
|
| 33 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 47 |
|
| 48 |
@spaces.GPU
|
| 49 |
def load_model_and_tokenizer():
|
| 50 |
+
"""Load 4-bit quantized model"""
|
| 51 |
try:
|
| 52 |
torch.cuda.empty_cache()
|
| 53 |
gc.collect()
|
|
|
|
| 72 |
print(f"Model loading error: {str(e)}")
|
| 73 |
raise
|
| 74 |
|
| 75 |
+
# ... [Keep the rest of your existing functions unchanged] ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
def process_diary(day, date, media):
|
| 78 |
+
"""Simplified processing pipeline"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
try:
|
| 80 |
if not media:
|
| 81 |
return [day, date, "No data", "No data", "No data", "No data", None]
|
|
|
|
| 130 |
model_activities = gr.Textbox(label="Activity Analysis", lines=4)
|
| 131 |
model_video = gr.Video(label="Safety Annotations")
|
| 132 |
|
| 133 |
+
# Fixed input mapping
|
| 134 |
submit_btn.click(
|
| 135 |
process_diary,
|
| 136 |
+
inputs=[day, date, media],
|
| 137 |
+
outputs=[
|
| 138 |
+
model_day,
|
| 139 |
+
model_date,
|
| 140 |
+
model_people,
|
| 141 |
+
model_machinery,
|
| 142 |
+
model_machinery_types,
|
| 143 |
+
model_activities,
|
| 144 |
+
model_video
|
| 145 |
+
]
|
| 146 |
)
|
| 147 |
|
| 148 |
if __name__ == "__main__":
|