karthikmn commited on
Commit
fe68a78
·
verified ·
1 Parent(s): 4fbfa19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -19
app.py CHANGED
@@ -9,6 +9,30 @@ import cv2
9
  import constants as const
10
  from get_drive_model import ensure_model_download
11
  import atexit # Import atexit module
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # Define the path for the directory
14
  FRAMES_FOLDER = 'static/frames'
@@ -30,7 +54,6 @@ FRAMES_FOLDER = 'static/frames'
30
  def process_video(filepath):
31
  print(f"Processing video: {filepath}")
32
 
33
- # Example: Read video frames and make predictions
34
  frames_for_prediction, frames_for_display = preprocess(filepath)
35
 
36
  print(f"Shape of frames for prediction: {frames_for_prediction.shape}")
@@ -38,7 +61,6 @@ def process_video(filepath):
38
 
39
  print(first_model.summary())
40
 
41
- # Anomaly detection prediction
42
  anomaly_prediction = first_model.predict(frames_for_prediction)[0][0]
43
  print(f"Anomaly Prediction: {anomaly_prediction}")
44
 
@@ -52,6 +74,10 @@ def process_video(filepath):
52
  prediction_label = f'No Anomalous Activity with {anomaly_prediction * 100:.2f}% confidence.'
53
 
54
  frame_paths = save_frames_to_filesystem(frames_for_display)
 
 
 
 
55
  return prediction_label, frame_paths
56
 
57
  def save_frames_to_filesystem(frames):
@@ -65,40 +91,37 @@ def save_frames_to_filesystem(frames):
65
  return frame_paths
66
 
67
  def cleanup_uploads_folder():
68
- # Clean up the UPLOAD_FOLDER
69
  if os.path.exists(UPLOAD_FOLDER):
70
  for filename in os.listdir(UPLOAD_FOLDER):
71
  file_path = os.path.join(UPLOAD_FOLDER, filename)
72
  try:
73
  if os.path.isfile(file_path) or os.path.islink(file_path):
74
- os.unlink(file_path) # Remove file or symbolic link
75
  elif os.path.isdir(file_path):
76
- shutil.rmtree(file_path) # Remove directory and its contents
77
  except Exception as e:
78
  print(f'Failed to delete {file_path}. Reason: {e}')
79
 
80
- # Clean up the FRAMES_FOLDER
81
  if os.path.exists(FRAMES_FOLDER):
82
  for filename in os.listdir(FRAMES_FOLDER):
83
  file_path = os.path.join(FRAMES_FOLDER, filename)
84
  try:
85
  if os.path.isfile(file_path) or os.path.islink(file_path):
86
- os.unlink(file_path) # Remove file or symbolic link
87
  elif os.path.isdir(file_path):
88
- shutil.rmtree(file_path) # Remove directory and its contents
89
  except Exception as e:
90
  print(f'Failed to delete {file_path}. Reason: {e}')
91
 
92
  print("Uploads and frames folders cleaned")
93
 
94
-
95
  # Register the cleanup function
96
  atexit.register(cleanup_uploads_folder)
97
 
98
  # Create Gradio Interface
99
  iface = gr.Interface(
100
  fn=process_video,
101
- inputs=gr.File(type="filepath"), # Add file upload for all detections
102
  outputs=[
103
  gr.Textbox(label="Prediction", elem_id="prediction-box"),
104
  gr.Gallery(label="Video Frames", elem_id="frame-gallery", columns=5, rows=10)
@@ -147,11 +170,10 @@ iface = gr.Interface(
147
  """
148
  )
149
 
150
- # Additional interfaces for individual triggers (with file upload option)
151
-
152
  violent_iface = gr.Interface(
153
- fn=process_video, # Use the same process_video for Violent Detection
154
- inputs=gr.File(type="filepath"), # File upload for violent detection
155
  outputs=[
156
  gr.Textbox(label="Prediction"),
157
  gr.Gallery(label="Video Frames", columns=5, rows=10)
@@ -160,8 +182,8 @@ violent_iface = gr.Interface(
160
  )
161
 
162
  explosion_iface = gr.Interface(
163
- fn=process_video, # Use the same process_video for Explosion Detection
164
- inputs=gr.File(type="filepath"), # File upload for explosion detection
165
  outputs=[
166
  gr.Textbox(label="Prediction"),
167
  gr.Gallery(label="Video Frames", columns=5, rows=10)
@@ -170,8 +192,8 @@ explosion_iface = gr.Interface(
170
  )
171
 
172
  normal_iface = gr.Interface(
173
- fn=process_video, # Use the same process_video for Normal Detection
174
- inputs=gr.File(type="filepath"), # File upload for normal detection
175
  outputs=[
176
  gr.Textbox(label="Prediction"),
177
  gr.Gallery(label="Video Frames", columns=5, rows=10)
@@ -181,9 +203,10 @@ normal_iface = gr.Interface(
181
 
182
  # Combine all interfaces into a single application
183
  combined_iface = gr.TabbedInterface([iface, violent_iface, explosion_iface, normal_iface],
184
- ["Upload Video", "Violent Detection", "Explosion Detection", "suspicious activities"])
185
 
186
  if __name__ == "__main__":
187
  combined_iface.launch()
188
 
189
 
 
 
9
  import constants as const
10
  from get_drive_model import ensure_model_download
11
  import atexit # Import atexit module
12
+ from simple_salesforce import Salesforce
13
+ from datetime import datetime
14
+
15
+ # Salesforce login
16
+ sf = Salesforce(
17
+ username='karthikm@sathkrutha.com',
18
+ password='Navya@1223',
19
+ security_token='FDWBkm0pbrNFkv6bwznbW1SKn',
20
+ domain='login' # use 'test' for sandbox, 'login' for production
21
+ )
22
+
23
+ # Salesforce object API name
24
+ SALESFORCE_OBJECT = 'Anomaly_Result__c'
25
+
26
+ def log_to_salesforce(video_path, prediction_text):
27
+ try:
28
+ result = sf.__getattr__(SALESFORCE_OBJECT).create({
29
+ 'Video_Name__c': os.path.basename(video_path),
30
+ 'Prediction_Result__c': prediction_text,
31
+ 'Timestamp__c': datetime.utcnow().isoformat()
32
+ })
33
+ print("Salesforce Record Created:", result)
34
+ except Exception as e:
35
+ print("Salesforce Logging Failed:", e)
36
 
37
  # Define the path for the directory
38
  FRAMES_FOLDER = 'static/frames'
 
54
  def process_video(filepath):
55
  print(f"Processing video: {filepath}")
56
 
 
57
  frames_for_prediction, frames_for_display = preprocess(filepath)
58
 
59
  print(f"Shape of frames for prediction: {frames_for_prediction.shape}")
 
61
 
62
  print(first_model.summary())
63
 
 
64
  anomaly_prediction = first_model.predict(frames_for_prediction)[0][0]
65
  print(f"Anomaly Prediction: {anomaly_prediction}")
66
 
 
74
  prediction_label = f'No Anomalous Activity with {anomaly_prediction * 100:.2f}% confidence.'
75
 
76
  frame_paths = save_frames_to_filesystem(frames_for_display)
77
+
78
+ # Log prediction to Salesforce
79
+ log_to_salesforce(filepath, prediction_label)
80
+
81
  return prediction_label, frame_paths
82
 
83
  def save_frames_to_filesystem(frames):
 
91
  return frame_paths
92
 
93
  def cleanup_uploads_folder():
 
94
  if os.path.exists(UPLOAD_FOLDER):
95
  for filename in os.listdir(UPLOAD_FOLDER):
96
  file_path = os.path.join(UPLOAD_FOLDER, filename)
97
  try:
98
  if os.path.isfile(file_path) or os.path.islink(file_path):
99
+ os.unlink(file_path)
100
  elif os.path.isdir(file_path):
101
+ shutil.rmtree(file_path)
102
  except Exception as e:
103
  print(f'Failed to delete {file_path}. Reason: {e}')
104
 
 
105
  if os.path.exists(FRAMES_FOLDER):
106
  for filename in os.listdir(FRAMES_FOLDER):
107
  file_path = os.path.join(FRAMES_FOLDER, filename)
108
  try:
109
  if os.path.isfile(file_path) or os.path.islink(file_path):
110
+ os.unlink(file_path)
111
  elif os.path.isdir(file_path):
112
+ shutil.rmtree(file_path)
113
  except Exception as e:
114
  print(f'Failed to delete {file_path}. Reason: {e}')
115
 
116
  print("Uploads and frames folders cleaned")
117
 
 
118
  # Register the cleanup function
119
  atexit.register(cleanup_uploads_folder)
120
 
121
  # Create Gradio Interface
122
  iface = gr.Interface(
123
  fn=process_video,
124
+ inputs=gr.File(type="filepath"),
125
  outputs=[
126
  gr.Textbox(label="Prediction", elem_id="prediction-box"),
127
  gr.Gallery(label="Video Frames", elem_id="frame-gallery", columns=5, rows=10)
 
170
  """
171
  )
172
 
173
+ # Additional interfaces for individual triggers
 
174
  violent_iface = gr.Interface(
175
+ fn=process_video,
176
+ inputs=gr.File(type="filepath"),
177
  outputs=[
178
  gr.Textbox(label="Prediction"),
179
  gr.Gallery(label="Video Frames", columns=5, rows=10)
 
182
  )
183
 
184
  explosion_iface = gr.Interface(
185
+ fn=process_video,
186
+ inputs=gr.File(type="filepath"),
187
  outputs=[
188
  gr.Textbox(label="Prediction"),
189
  gr.Gallery(label="Video Frames", columns=5, rows=10)
 
192
  )
193
 
194
  normal_iface = gr.Interface(
195
+ fn=process_video,
196
+ inputs=gr.File(type="filepath"),
197
  outputs=[
198
  gr.Textbox(label="Prediction"),
199
  gr.Gallery(label="Video Frames", columns=5, rows=10)
 
203
 
204
  # Combine all interfaces into a single application
205
  combined_iface = gr.TabbedInterface([iface, violent_iface, explosion_iface, normal_iface],
206
+ ["Upload Video", "Violent Detection", "Explosion Detection", "Suspicious Activities"])
207
 
208
  if __name__ == "__main__":
209
  combined_iface.launch()
210
 
211
 
212
+