CosmickVisions commited on
Commit
42e90f4
Β·
verified Β·
1 Parent(s): 5c36573

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -10
app.py CHANGED
@@ -21,6 +21,7 @@ import time
21
  import matplotlib.pyplot as plt
22
  from pathlib import Path
23
  import plotly.express as px
 
24
 
25
  # Set page config
26
  st.set_page_config(
@@ -515,6 +516,33 @@ def process_video_file(video_file, analysis_types):
515
  "labels": {}
516
  }
517
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
  try:
519
  frame_count = 0
520
  while frame_count < max_frames: # Limit to 10 seconds
@@ -697,7 +725,10 @@ def process_video_file(video_file, analysis_types):
697
  os.unlink(output_path)
698
 
699
  # Return both the video and the detection statistics
700
- return processed_video_bytes, detection_stats
 
 
 
701
 
702
  except Exception as e:
703
  # Clean up on error
@@ -959,6 +990,8 @@ def main():
959
  analysis_types.append("Face Detection")
960
  if st.sidebar.checkbox("Text Recognition"):
961
  analysis_types.append("Text")
 
 
962
 
963
  st.sidebar.markdown("---")
964
  st.sidebar.warning("⚠️ Video analysis may use a significant amount of API calls. Use responsibly.")
@@ -996,7 +1029,7 @@ def main():
996
  with st.spinner("Processing video (max 10 seconds)..."):
997
  try:
998
  # Process the video with enhanced detail
999
- processed_video, detection_stats = process_video_file(uploaded_file, analysis_types)
1000
 
1001
  if processed_video:
1002
  # Offer download of processed video
@@ -1015,11 +1048,11 @@ def main():
1015
  st.markdown("### Detailed Analysis Results")
1016
 
1017
  # Display object detection summary
1018
- if "Objects" in analysis_types and detection_stats["objects"]:
1019
  st.markdown("#### πŸ“¦ Objects Detected")
1020
 
1021
  # Sort objects by frequency
1022
- sorted_objects = dict(sorted(detection_stats["objects"].items(),
1023
  key=lambda x: x[1], reverse=True))
1024
 
1025
  # Create bar chart for objects
@@ -1040,21 +1073,21 @@ def main():
1040
  st.markdown(f"- {obj}: {count} occurrences")
1041
 
1042
  # Display face detection summary
1043
- if "Face Detection" in analysis_types and detection_stats["faces"] > 0:
1044
  st.markdown("#### πŸ‘€ Face Analysis")
1045
- st.markdown(f"Total faces detected: {detection_stats['faces']}")
1046
 
1047
  # Display text detection summary
1048
- if "Text" in analysis_types and detection_stats["text_blocks"] > 0:
1049
  st.markdown("#### πŸ“ Text Analysis")
1050
- st.markdown(f"Total text blocks detected: {detection_stats['text_blocks']}")
1051
 
1052
  # Display label detection summary
1053
- if "Labels" in analysis_types and detection_stats["labels"]:
1054
  st.markdown("#### 🏷️ Scene Labels")
1055
 
1056
  # Sort labels by frequency
1057
- sorted_labels = dict(sorted(detection_stats["labels"].items(),
1058
  key=lambda x: x[1], reverse=True))
1059
 
1060
  # Create pie chart for top labels
@@ -1068,6 +1101,25 @@ def main():
1068
  ax.pie(top_labels.values(), labels=top_labels.keys(), autopct='%1.1f%%')
1069
  ax.set_title('Distribution of Scene Labels')
1070
  st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1071
 
1072
  except Exception as e:
1073
  st.error(f"Error processing video: {str(e)}")
 
21
  import matplotlib.pyplot as plt
22
  from pathlib import Path
23
  import plotly.express as px
24
+ from google.cloud import videointelligence
25
 
26
  # Set page config
27
  st.set_page_config(
 
516
  "labels": {}
517
  }
518
 
519
+ # Add Video Intelligence API integration for semantic analysis
520
+ if "Semantic" in analysis_types:
521
+ # Initialize video intelligence client
522
+ video_client = videointelligence.VideoIntelligenceServiceClient(credentials=credentials)
523
+
524
+ # Set up features for semantic analysis
525
+ features = [
526
+ videointelligence.Feature.LABEL_DETECTION,
527
+ videointelligence.Feature.SHOT_CHANGE_DETECTION,
528
+ videointelligence.Feature.ACTION_RECOGNITION
529
+ ]
530
+
531
+ # Process video for semantic understanding
532
+ operation = video_client.annotate_video(
533
+ request={"features": features, "input_content": video_file.read()}
534
+ )
535
+
536
+ # Get semantic results
537
+ semantic_results = operation.result()
538
+
539
+ # Store semantic insights for visualization
540
+ semantic_insights = {
541
+ "actions": extract_actions(semantic_results),
542
+ "segments": extract_segments(semantic_results),
543
+ "scene_labels": extract_labels(semantic_results)
544
+ }
545
+
546
  try:
547
  frame_count = 0
548
  while frame_count < max_frames: # Limit to 10 seconds
 
725
  os.unlink(output_path)
726
 
727
  # Return both the video and the detection statistics
728
+ return processed_video_bytes, {
729
+ "detection_stats": detection_stats,
730
+ "semantic_insights": semantic_insights if "Semantic" in analysis_types else None
731
+ }
732
 
733
  except Exception as e:
734
  # Clean up on error
 
990
  analysis_types.append("Face Detection")
991
  if st.sidebar.checkbox("Text Recognition"):
992
  analysis_types.append("Text")
993
+ if st.sidebar.checkbox("Semantic Analysis", value=True):
994
+ analysis_types.append("Semantic")
995
 
996
  st.sidebar.markdown("---")
997
  st.sidebar.warning("⚠️ Video analysis may use a significant amount of API calls. Use responsibly.")
 
1029
  with st.spinner("Processing video (max 10 seconds)..."):
1030
  try:
1031
  # Process the video with enhanced detail
1032
+ processed_video, results = process_video_file(uploaded_file, analysis_types)
1033
 
1034
  if processed_video:
1035
  # Offer download of processed video
 
1048
  st.markdown("### Detailed Analysis Results")
1049
 
1050
  # Display object detection summary
1051
+ if "Objects" in analysis_types and results["detection_stats"]["objects"]:
1052
  st.markdown("#### πŸ“¦ Objects Detected")
1053
 
1054
  # Sort objects by frequency
1055
+ sorted_objects = dict(sorted(results["detection_stats"]["objects"].items(),
1056
  key=lambda x: x[1], reverse=True))
1057
 
1058
  # Create bar chart for objects
 
1073
  st.markdown(f"- {obj}: {count} occurrences")
1074
 
1075
  # Display face detection summary
1076
+ if "Face Detection" in analysis_types and results["detection_stats"]["faces"] > 0:
1077
  st.markdown("#### πŸ‘€ Face Analysis")
1078
+ st.markdown(f"Total faces detected: {results['detection_stats']['faces']}")
1079
 
1080
  # Display text detection summary
1081
+ if "Text" in analysis_types and results["detection_stats"]["text_blocks"] > 0:
1082
  st.markdown("#### πŸ“ Text Analysis")
1083
+ st.markdown(f"Total text blocks detected: {results['detection_stats']['text_blocks']}")
1084
 
1085
  # Display label detection summary
1086
+ if "Labels" in analysis_types and results["detection_stats"]["labels"]:
1087
  st.markdown("#### 🏷️ Scene Labels")
1088
 
1089
  # Sort labels by frequency
1090
+ sorted_labels = dict(sorted(results["detection_stats"]["labels"].items(),
1091
  key=lambda x: x[1], reverse=True))
1092
 
1093
  # Create pie chart for top labels
 
1101
  ax.pie(top_labels.values(), labels=top_labels.keys(), autopct='%1.1f%%')
1102
  ax.set_title('Distribution of Scene Labels')
1103
  st.pyplot(fig)
1104
+
1105
+ # Display semantic analysis results
1106
+ if "Semantic" in analysis_types and results["semantic_insights"]:
1107
+ st.markdown("### 🧠 Semantic Understanding")
1108
+
1109
+ # Display scene context
1110
+ st.markdown("#### Video Context")
1111
+ for label in results["semantic_insights"]["scene_labels"][:10]:
1112
+ st.markdown(f"- {label['description']}: {label['confidence']:.1%}")
1113
+
1114
+ # Display activities detected
1115
+ st.markdown("#### Activities")
1116
+ for action in results["semantic_insights"]["actions"]:
1117
+ st.markdown(f"- {action['description']} at {action['time_segment']}")
1118
+
1119
+ # Visualize relationships and context
1120
+ st.markdown("#### Scene Segments")
1121
+ fig = create_segment_visualization(results["semantic_insights"]["segments"])
1122
+ st.plotly_chart(fig)
1123
 
1124
  except Exception as e:
1125
  st.error(f"Error processing video: {str(e)}")