omarrama555 commited on
Commit
2ef84be
·
verified ·
1 Parent(s): 587ff62

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +29 -27
streamlit_app.py CHANGED
@@ -359,42 +359,44 @@ else:
359
  st.write("Connect a live camera to detect solar panel defects in real-time.")
360
 
361
  if WEBRTC_AVAILABLE and model:
362
- st.warning("For live camera to work in Colab, you might need to grant camera permissions in your browser. Performance may vary.")
363
 
364
- class VideoProcessor(VideoTransformerBase):
365
- def __init__(self, model):
366
- self.model = model
367
 
368
- def recv(self, frame):
369
- img = frame.to_ndarray(format="bgr24")
 
370
 
371
- # Perform inference
372
- results = self.model.predict(img, conf=0.5, verbose=False)
 
373
 
374
- # Annotate the image
375
- annotated_img = img.copy()
376
- for r in results:
377
- annotated_img = r.plot() # YOLO's plot method returns annotated image
378
-
379
- return frame.from_ndarray(annotated_img, format="bgr24")
380
 
381
  webrtc_streamer(
382
- key="live_camera_analysis",
383
- video_transformer_factory=lambda: VideoProcessor(model),
384
- client_settings=ClientSettings(
385
- rtc_configuration={
386
- "iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]}
387
- ,
388
- media_stream_constraints={
389
- "video": True,
390
- "audio": False,
391
- },
392
- ),
393
  )
 
 
 
394
  elif not WEBRTC_AVAILABLE:
395
- st.error("streamlit_webrtc library is not installed. Please install it using `!pip install streamlit-webrtc`.")
396
  else:
397
- st.info("Model not loaded or not available for live analysis.")
 
 
398
 
399
  # ---------------- VIDEO ANALYSIS ----------------
400
  elif menu == "Video Analysis":
 
359
  st.write("Connect a live camera to detect solar panel defects in real-time.")
360
 
361
  if WEBRTC_AVAILABLE and model:
362
+ import av # تأكد من إضافة av في ملف requirements.txt
363
 
364
+ def video_frame_callback(frame):
365
+ # تحويل الفريم إلى مصفوفة Numpy (BGR)
366
+ img = frame.to_ndarray(format="bgr24")
367
 
368
+ # إجراء الكشف باستخدام الموديل
369
+ # نستخدم الموديل مباشرة لتحسين الأداء
370
+ results = model(img, conf=0.5, verbose=False)
371
 
372
+ # رسم المربعات والنتائج (Annotate)
373
+ # YOLOv8 توفر ميزة .plot() التي تعود بالصورة مرسوم عليها النتائج
374
+ annotated_img = results[0].plot()
375
 
376
+ # إعادة الفريم إلى تنسيق الفيديو المطلوب للمتصفح
377
+ return av.VideoFrame.from_ndarray(annotated_img, format="bgr24")
 
 
 
 
378
 
379
  webrtc_streamer(
380
+ key="solar-guard-live",
381
+ video_frame_callback=video_frame_callback,
382
+ rtc_configuration={
383
+ "iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
384
+ },
385
+ media_stream_constraints={
386
+ "video": True,
387
+ "audio": False,
388
+ },
389
+ async_processing=True, # لتحسين سرعة الفيديو
 
390
  )
391
+
392
+ st.info("💡 اضغط على Start لبدء تشغيل الكاميرا. قد يطلب المتصفح إذن الوصول للكاميرا.")
393
+
394
  elif not WEBRTC_AVAILABLE:
395
+ st.error("⚠️ مكتبة streamlit_webrtc غير مثبتة. تأكد من إضافتها لملف requirements.txt")
396
  else:
397
+ st.error("⚠️ الموديل (best.pt) لم يتم تحميله بشكل صحيح. تأكد من رفعه باستخدام Git LFS كما ذكرنا.")
398
+
399
+
400
 
401
  # ---------------- VIDEO ANALYSIS ----------------
402
  elif menu == "Video Analysis":