Abs6187 commited on
Commit
06372a9
1 Parent(s): 70b11da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -30
app.py CHANGED
@@ -2,50 +2,30 @@ import cv2
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
- # Load Hugging Face models
6
- pose_detection = pipeline("object-detection", model="./yolov8-pose") # Local YOLOv8 pose model
7
- suspicious_activity_detection = pipeline("text-classification", model="./suspicious_activity_model") # Local LSTM model
8
 
9
- # Function to process a single frame and predict suspicious activity
10
  def process_frame(frame):
11
- """
12
- Detect persons in the frame and determine if they exhibit suspicious behavior.
13
- """
14
  results = pose_detection(frame)
15
 
16
  for person in results:
17
- if person['label'] == 'person' and 'box' in person:
18
  x1, y1, x2, y2 = map(int, person['box'].values())
19
-
20
- if 'keypoints' in person:
21
- keypoints = person['keypoints']
22
- keypoints_input = " ".join(map(str, [kp for point in keypoints for kp in point[:2]]))
23
-
24
- # Predict suspicious activity
25
- prediction = suspicious_activity_detection(keypoints_input)[0]['label']
26
- color = (0, 0, 255) if prediction == "Suspicious" else (0, 255, 0)
27
- label = 'Suspicious' if prediction == "Suspicious" else 'Normal'
28
-
29
- # Annotate frame
30
- cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
31
- cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
32
- else:
33
- print("No keypoints found for detected person.")
34
  return frame
35
 
36
- # Gradio interface
37
  def live_detection(frame):
38
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert BGR to RGB for Gradio
39
  processed_frame = process_frame(frame)
40
- return cv2.cvtColor(processed_frame, cv2.COLOR_RGB2BGR) # Convert back to BGR for display
41
 
42
- # Gradio UI
43
  interface = gr.Interface(
44
  fn=live_detection,
45
  inputs=gr.Image(source="webcam", tool="editor", type="numpy"),
46
- outputs=gr.Image(type="numpy", label="Processed Video Stream"),
47
- live=True,
48
- description="Real-time Suspicious Activity Detection"
49
  )
50
 
51
  if __name__ == "__main__":
 
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
+ # Load YOLOv8 model and suspicious activity classification model
6
+ pose_detection = pipeline("object-detection", model="yolov8-pose") # Correct path if it's inside same folder.
7
+ suspicious_activity_detection = pipeline("text-classification", model="suspicious_activity_model")
8
 
 
9
  def process_frame(frame):
 
 
 
10
  results = pose_detection(frame)
11
 
12
  for person in results:
13
+ if person['label'] == 'person':
14
  x1, y1, x2, y2 = map(int, person['box'].values())
15
+ cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
16
+ cv2.putText(frame, 'Detected', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  return frame
18
 
 
19
  def live_detection(frame):
20
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
21
  processed_frame = process_frame(frame)
22
+ return processed_frame
23
 
 
24
  interface = gr.Interface(
25
  fn=live_detection,
26
  inputs=gr.Image(source="webcam", tool="editor", type="numpy"),
27
+ outputs=gr.Image(type="numpy"),
28
+ live=True
 
29
  )
30
 
31
  if __name__ == "__main__":