Haiss123 commited on
Commit
ee3f39a
·
verified ·
1 Parent(s): d71df34

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +75 -121
main.py CHANGED
@@ -74,11 +74,11 @@ class ContentModerator:
74
  return {
75
  'weapon_detection': {
76
  'enabled': True,
77
- 'confidence_threshold': 0.45, # For guns
78
- 'knife_confidence': 0.45, # Lower threshold for knives
79
- 'fight_confidence': 0.40, # Lower threshold for fights (behavioral)
80
- 'model_size': 'yolo11n',
81
- 'classes': ['knife', 'gun', 'rifle', 'pistol', 'weapon', 'fight'],
82
  'use_enhancement': True, # Enable image enhancement for knives
83
  'multi_pass': True, # Enable multi-pass detection
84
  'boost_knife_detection': True, # Enable knife confidence boosting
@@ -87,10 +87,10 @@ class ContentModerator:
87
  },
88
  'fight_detection': {
89
  'enabled': True,
90
- 'confidence_threshold': 0.40,
91
  'pose_analysis': True, # Analyze poses for fighting
92
  'motion_analysis': False, # Motion-based fight detection (for video)
93
- 'aggression_keywords': ['fight', 'violence', 'aggression', 'combat'],
94
  'threat_escalation': True, # Escalate threat level for fights
95
  'multi_person_analysis': True # Analyze interactions between people
96
  },
@@ -141,7 +141,7 @@ class ContentModerator:
141
  print("🔫 Loading weapon and fight detection models...")
142
 
143
  # Model 1: Custom YOLO11 for weapons (dao + súng + fight)
144
- custom_model_path = "models/best.pt"
145
  project_root = os.path.dirname(os.path.abspath(__file__))
146
  full_model_path = os.path.join(project_root, custom_model_path)
147
 
@@ -204,7 +204,16 @@ class ContentModerator:
204
  if self.config['weapon_detection']['use_enhancement']:
205
  enhanced_image = self.enhance_knife_detection(image)
206
  images_to_process.append((enhanced_image, 1.15, "enhanced"))
207
-
 
 
 
 
 
 
 
 
 
208
  # Process each image version
209
  for img, weight_multiplier, img_type in images_to_process:
210
  if self.weapon_model_custom:
@@ -564,8 +573,6 @@ class ContentModerator:
564
 
565
  return detections
566
 
567
- # ... (rest of the existing methods remain the same) ...
568
-
569
  def enhance_knife_detection(self, image):
570
  """Enhance image specifically for better knife/dao detection"""
571
  try:
@@ -823,8 +830,6 @@ class ContentModerator:
823
 
824
  return intersect_area / union_area if union_area > 0 else 0
825
 
826
- # ... (continue with remaining NSFW detection methods) ...
827
-
828
  def detect_nsfw_content(self, image):
829
  """Enhanced NSFW detection with person detection first"""
830
  detections = []
@@ -1127,8 +1132,14 @@ class ContentModerator:
1127
  """Clean expired cache entries"""
1128
  try:
1129
  expired_keys = []
1130
- for key, (_, timestamp) in self.detection_cache.items():
1131
- if current_time - timestamp > self.cache_ttl:
 
 
 
 
 
 
1132
  expired_keys.append(key)
1133
 
1134
  for key in expired_keys:
@@ -1281,14 +1292,13 @@ class ContentModerator:
1281
  print(f"❌ Error drawing detections: {e}")
1282
  return image
1283
 
1284
- def process_video(self, video_path, output_path=None, frame_skip=2):
1285
- """Process video file with enhanced detection including fights - optimized frame processing"""
1286
  try:
1287
  cap = cv2.VideoCapture(video_path)
1288
  frame_count = 0
1289
  total_detections = []
1290
  fight_timeline = [] # Track fights over time
1291
- recent_detections = [] # Track recent detections for adaptive processing
1292
 
1293
  if output_path:
1294
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
@@ -1304,22 +1314,7 @@ class ContentModerator:
1304
 
1305
  frame_count += 1
1306
 
1307
- # Adaptive frame processing based on recent detections
1308
- should_process = False
1309
-
1310
- # Always process if recent threats detected (within last 10 frames)
1311
- if any(det['frame'] > frame_count - 10 for det in recent_detections[-5:]):
1312
- should_process = True
1313
- # Or process based on reduced skip rate
1314
- elif frame_count % max(1, frame_skip) == 0:
1315
- should_process = True
1316
-
1317
- if not should_process:
1318
- if output_path:
1319
- out.write(frame)
1320
- continue
1321
-
1322
- # Process frame
1323
  result = self.process_image(frame)
1324
  if result and result['detections']:
1325
  # Add frame number to each detection for tracking
@@ -1327,7 +1322,6 @@ class ContentModerator:
1327
  detection['frame'] = frame_count
1328
 
1329
  total_detections.extend(result['detections'])
1330
- recent_detections.append({'frame': frame_count, 'count': len(result['detections'])})
1331
 
1332
  # Track fight timeline
1333
  fight_detections = [d for d in result['detections'] if d['type'] == 'fight']
@@ -1340,9 +1334,6 @@ class ContentModerator:
1340
  'max_aggression': max(f.get('aggression_level', 'low') for f in fight_detections)
1341
  })
1342
 
1343
- # Reduce frame_skip temporarily when fight detected
1344
- frame_skip = max(1, frame_skip // 2)
1345
-
1346
  print(f"⚠️ Frame {frame_count}: {len(result['detections'])} threats detected")
1347
 
1348
  breakdown = result.get('detection_breakdown', {})
@@ -1354,10 +1345,6 @@ class ContentModerator:
1354
  elif output_path:
1355
  out.write(frame)
1356
  else:
1357
- # No detections - can increase frame_skip for efficiency
1358
- if len(recent_detections) > 5 and all(det['count'] == 0 for det in recent_detections[-5:]):
1359
- frame_skip = min(5, frame_skip + 1)
1360
-
1361
  if output_path:
1362
  out.write(frame)
1363
 
@@ -1378,7 +1365,7 @@ class ContentModerator:
1378
  }
1379
 
1380
  return {
1381
- 'total_frames_processed': frame_count // frame_skip,
1382
  'total_detections': len(total_detections),
1383
  'detections': total_detections,
1384
  'fight_timeline': fight_timeline,
@@ -1413,7 +1400,7 @@ class ContentModerator:
1413
 
1414
 
1415
  def main():
1416
- """Enhanced example usage with knife and fight detection improvements"""
1417
 
1418
  # Initialize the system
1419
  moderator = ContentModerator()
@@ -1544,7 +1531,7 @@ def main():
1544
  result = moderator.process_image(test_img)
1545
  print("✅ Test pattern processed successfully")
1546
 
1547
- # Example 2: Enhanced webcam processing with fight detection
1548
  print("\n" + "=" * 60)
1549
  print("📹 ENHANCED WEBCAM PROCESSING WITH FIGHT DETECTION")
1550
  print("=" * 60)
@@ -1577,11 +1564,6 @@ def main():
1577
  'fight_incidents': 0
1578
  }
1579
 
1580
- # Adaptive processing variables
1581
- process_interval = 2 # Start with every 2nd frame
1582
- last_detection_frame = 0
1583
- consecutive_safe_frames = 0
1584
-
1585
  while True:
1586
  ret, frame = cap.read()
1587
  if not ret:
@@ -1610,81 +1592,52 @@ def main():
1610
  cv2.putText(frame, model_info, (10, y_offset + 60),
1611
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
1612
 
1613
- # Adaptive frame processing - process more frequently when threats detected
1614
- should_process = False
1615
-
1616
- # Always process if recent threats (within last 30 frames)
1617
- if frame_count - last_detection_frame <= 30:
1618
- should_process = (frame_count % 1 == 0) # Process every frame
1619
- cv2.putText(frame, "HIGH ALERT MODE", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
1620
- # Normal processing with reduced interval
1621
- elif frame_count % process_interval == 0:
1622
- should_process = True
1623
-
1624
- if should_process:
1625
- result = moderator.process_image(frame)
1626
-
1627
- if result and result['action_required']:
1628
- last_detection_frame = frame_count # Update last detection frame
1629
- consecutive_safe_frames = 0
1630
- process_interval = 1 # Process every frame when threats detected
1631
-
1632
- # Count detections by type
1633
- for detection in result['detections']:
1634
- if detection['type'] == 'weapon':
1635
- detection_stats['weapons'] += 1
1636
- if detection['weapon_type'] == 'blade':
1637
- detection_stats['knives'] += 1
1638
- elif detection['weapon_type'] == 'firearm':
1639
- detection_stats['guns'] += 1
1640
- elif detection['type'] == 'fight':
1641
- detection_stats['fights'] += 1
1642
- if detection.get('aggression_level') in ['high', 'extreme']:
1643
- detection_stats['fight_incidents'] += 1
1644
- elif detection['type'] == 'nsfw':
1645
- detection_stats['nsfw'] += 1
1646
-
1647
- print(
1648
- f"⚠️ Frame {frame_count}: {result['risk_level']} risk - {result['total_threats']} threats!")
1649
-
1650
- # Show specific detections with fight info
1651
- for detection in result['detections']:
1652
- if detection['type'] == 'weapon':
1653
- icon = "🔪" if detection['weapon_type'] == 'blade' else "🔫"
1654
- method = detection.get('detection_method', 'unknown').split('_')[-1]
1655
- print(f" {icon} {detection['class']} ({detection['confidence']:.3f}) [{method}]")
1656
- elif detection['type'] == 'fight':
1657
- fight_type = detection.get('fight_type', 'general')
1658
- aggression = detection.get('aggression_level', 'unknown')
1659
- people = detection.get('people_involved', 0)
1660
- method = detection.get('detection_method', 'unknown').split('_')[-1]
1661
- print(f" 👊 FIGHT: {fight_type} ({detection['confidence']:.3f}) [{method}]")
1662
- print(f" Aggression: {aggression}, People: {people}")
1663
-
1664
- # Use annotated frame
1665
- if 'annotated_image' in result:
1666
- cv2.imshow('Enhanced Detection System (Weapons + Fights)', result['annotated_image'])
1667
- else:
1668
- # Add threat counter
1669
- breakdown = result.get('detection_breakdown', {})
1670
- threat_text = f"THREATS: W:{breakdown.get('weapons', 0)} F:{breakdown.get('fights', 0)} N:{breakdown.get('nsfw', 0)}"
1671
- cv2.putText(frame, threat_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
1672
- cv2.imshow('Enhanced Detection System (Weapons + Fights)', frame)
1673
  else:
1674
- consecutive_safe_frames += 1
1675
- # Gradually increase processing interval when safe (up to max 3)
1676
- if consecutive_safe_frames > 30:
1677
- process_interval = min(3, process_interval + 1)
1678
- consecutive_safe_frames = 0
1679
-
1680
- cv2.putText(frame, "SAFE", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
1681
- cv2.putText(frame, f"Process Interval: {process_interval}", (10, 90),
1682
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
1683
  cv2.imshow('Enhanced Detection System (Weapons + Fights)', frame)
1684
  else:
1685
  cv2.putText(frame, "SAFE", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
1686
- cv2.putText(frame, f"Process Interval: {process_interval}", (10, 90),
1687
- cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
1688
  cv2.imshow('Enhanced Detection System (Weapons + Fights)', frame)
1689
 
1690
  # Handle key presses
@@ -1778,3 +1731,4 @@ def main():
1778
  print(" - Dynamic confidence thresholds")
1779
  print(" - Geometric feature analysis")
1780
  print(" - Multi-pass detection strategy")
 
 
74
  return {
75
  'weapon_detection': {
76
  'enabled': True,
77
+ 'confidence_threshold': 0.5, # For guns
78
+ 'knife_confidence': 0.5, # Lower threshold for knives
79
+ 'fight_confidence': 0.45, # Lower threshold for fights (behavioral)
80
+ 'model_size': 'yolo12n',
81
+ 'classes': ['gun', 'knife', 'fight'],
82
  'use_enhancement': True, # Enable image enhancement for knives
83
  'multi_pass': True, # Enable multi-pass detection
84
  'boost_knife_detection': True, # Enable knife confidence boosting
 
87
  },
88
  'fight_detection': {
89
  'enabled': True,
90
+ 'confidence_threshold': 0.45,
91
  'pose_analysis': True, # Analyze poses for fighting
92
  'motion_analysis': False, # Motion-based fight detection (for video)
93
+ 'aggression_keywords': ['fight'],
94
  'threat_escalation': True, # Escalate threat level for fights
95
  'multi_person_analysis': True # Analyze interactions between people
96
  },
 
141
  print("🔫 Loading weapon and fight detection models...")
142
 
143
  # Model 1: Custom YOLO11 for weapons (dao + súng + fight)
144
+ custom_model_path = "models/best_ft4.pt"
145
  project_root = os.path.dirname(os.path.abspath(__file__))
146
  full_model_path = os.path.join(project_root, custom_model_path)
147
 
 
204
  if self.config['weapon_detection']['use_enhancement']:
205
  enhanced_image = self.enhance_knife_detection(image)
206
  images_to_process.append((enhanced_image, 1.15, "enhanced"))
207
+ if 'fight_detection' not in self.config:
208
+ self.config['fight_detection'] = {
209
+ 'enabled': True,
210
+ 'confidence_threshold': 0.40,
211
+ 'pose_analysis': False,
212
+ 'motion_analysis': False,
213
+ 'aggression_keywords': ['fight'],
214
+ 'threat_escalation': True,
215
+ 'multi_person_analysis': False
216
+ }
217
  # Process each image version
218
  for img, weight_multiplier, img_type in images_to_process:
219
  if self.weapon_model_custom:
 
573
 
574
  return detections
575
 
 
 
576
  def enhance_knife_detection(self, image):
577
  """Enhance image specifically for better knife/dao detection"""
578
  try:
 
830
 
831
  return intersect_area / union_area if union_area > 0 else 0
832
 
 
 
833
  def detect_nsfw_content(self, image):
834
  """Enhanced NSFW detection with person detection first"""
835
  detections = []
 
1132
  """Clean expired cache entries"""
1133
  try:
1134
  expired_keys = []
1135
+ for key, value in self.detection_cache.items():
1136
+ # Check tuple structure
1137
+ if isinstance(value, tuple) and len(value) == 2:
1138
+ _, timestamp = value
1139
+ if timestamp is not None and current_time - timestamp > self.cache_ttl:
1140
+ expired_keys.append(key)
1141
+ else:
1142
+ # Invalid cache entry, remove it
1143
  expired_keys.append(key)
1144
 
1145
  for key in expired_keys:
 
1292
  print(f"❌ Error drawing detections: {e}")
1293
  return image
1294
 
1295
+ def process_video(self, video_path, output_path=None):
1296
+ """Process video file with enhanced detection including fights - processes every frame"""
1297
  try:
1298
  cap = cv2.VideoCapture(video_path)
1299
  frame_count = 0
1300
  total_detections = []
1301
  fight_timeline = [] # Track fights over time
 
1302
 
1303
  if output_path:
1304
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
 
1314
 
1315
  frame_count += 1
1316
 
1317
+ # Process every frame
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1318
  result = self.process_image(frame)
1319
  if result and result['detections']:
1320
  # Add frame number to each detection for tracking
 
1322
  detection['frame'] = frame_count
1323
 
1324
  total_detections.extend(result['detections'])
 
1325
 
1326
  # Track fight timeline
1327
  fight_detections = [d for d in result['detections'] if d['type'] == 'fight']
 
1334
  'max_aggression': max(f.get('aggression_level', 'low') for f in fight_detections)
1335
  })
1336
 
 
 
 
1337
  print(f"⚠️ Frame {frame_count}: {len(result['detections'])} threats detected")
1338
 
1339
  breakdown = result.get('detection_breakdown', {})
 
1345
  elif output_path:
1346
  out.write(frame)
1347
  else:
 
 
 
 
1348
  if output_path:
1349
  out.write(frame)
1350
 
 
1365
  }
1366
 
1367
  return {
1368
+ 'total_frames_processed': frame_count,
1369
  'total_detections': len(total_detections),
1370
  'detections': total_detections,
1371
  'fight_timeline': fight_timeline,
 
1400
 
1401
 
1402
  def main():
1403
+ """Enhanced example usage with knife and fight detection improvements - processes every frame"""
1404
 
1405
  # Initialize the system
1406
  moderator = ContentModerator()
 
1531
  result = moderator.process_image(test_img)
1532
  print("✅ Test pattern processed successfully")
1533
 
1534
+ # Example 2: Enhanced webcam processing with fight detection - processes every frame
1535
  print("\n" + "=" * 60)
1536
  print("📹 ENHANCED WEBCAM PROCESSING WITH FIGHT DETECTION")
1537
  print("=" * 60)
 
1564
  'fight_incidents': 0
1565
  }
1566
 
 
 
 
 
 
1567
  while True:
1568
  ret, frame = cap.read()
1569
  if not ret:
 
1592
  cv2.putText(frame, model_info, (10, y_offset + 60),
1593
  cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
1594
 
1595
+ # Process every frame
1596
+ result = moderator.process_image(frame)
1597
+
1598
+ if result and result['action_required']:
1599
+ # Count detections by type
1600
+ for detection in result['detections']:
1601
+ if detection['type'] == 'weapon':
1602
+ detection_stats['weapons'] += 1
1603
+ if detection['weapon_type'] == 'blade':
1604
+ detection_stats['knives'] += 1
1605
+ elif detection['weapon_type'] == 'firearm':
1606
+ detection_stats['guns'] += 1
1607
+ elif detection['type'] == 'fight':
1608
+ detection_stats['fights'] += 1
1609
+ if detection.get('aggression_level') in ['high', 'extreme']:
1610
+ detection_stats['fight_incidents'] += 1
1611
+ elif detection['type'] == 'nsfw':
1612
+ detection_stats['nsfw'] += 1
1613
+
1614
+ print(f"⚠️ Frame {frame_count}: {result['risk_level']} risk - {result['total_threats']} threats!")
1615
+
1616
+ # Show specific detections with fight info
1617
+ for detection in result['detections']:
1618
+ if detection['type'] == 'weapon':
1619
+ icon = "🔪" if detection['weapon_type'] == 'blade' else "🔫"
1620
+ method = detection.get('detection_method', 'unknown').split('_')[-1]
1621
+ print(f" {icon} {detection['class']} ({detection['confidence']:.3f}) [{method}]")
1622
+ elif detection['type'] == 'fight':
1623
+ fight_type = detection.get('fight_type', 'general')
1624
+ aggression = detection.get('aggression_level', 'unknown')
1625
+ people = detection.get('people_involved', 0)
1626
+ method = detection.get('detection_method', 'unknown').split('_')[-1]
1627
+ print(f" 👊 FIGHT: {fight_type} ({detection['confidence']:.3f}) [{method}]")
1628
+ print(f" Aggression: {aggression}, People: {people}")
1629
+
1630
+ # Use annotated frame
1631
+ if 'annotated_image' in result:
1632
+ cv2.imshow('Enhanced Detection System (Weapons + Fights)', result['annotated_image'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1633
  else:
1634
+ # Add threat counter
1635
+ breakdown = result.get('detection_breakdown', {})
1636
+ threat_text = f"THREATS: W:{breakdown.get('weapons', 0)} F:{breakdown.get('fights', 0)} N:{breakdown.get('nsfw', 0)}"
1637
+ cv2.putText(frame, threat_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
 
 
 
 
 
1638
  cv2.imshow('Enhanced Detection System (Weapons + Fights)', frame)
1639
  else:
1640
  cv2.putText(frame, "SAFE", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
 
 
1641
  cv2.imshow('Enhanced Detection System (Weapons + Fights)', frame)
1642
 
1643
  # Handle key presses
 
1731
  print(" - Dynamic confidence thresholds")
1732
  print(" - Geometric feature analysis")
1733
  print(" - Multi-pass detection strategy")
1734
+ print("💡 Processing mode: EVERY FRAME (no skipping)")