capradeepgujaran commited on
Commit
27eab0f
·
verified ·
1 Parent(s): 30f4028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
app.py CHANGED
@@ -6,6 +6,7 @@ import time
6
  from PIL import Image as PILImage
7
  import io
8
  import os
 
9
 
10
  def create_monitor_interface():
11
  api_key = os.getenv("GROQ_API_KEY")
@@ -38,10 +39,18 @@ def create_monitor_interface():
38
  if frame is None:
39
  return "No frame received"
40
 
 
 
 
 
 
 
41
  frame_pil = PILImage.fromarray(frame)
42
- img_byte_arr = io.BytesIO()
43
- frame_pil.save(img_byte_arr, format='JPEG')
44
- img_byte_arr = img_byte_arr.getvalue()
 
 
45
 
46
  prompt = """Analyze this image for workplace safety issues. Focus on:
47
  1. PPE usage (helmets, vests, etc.)
@@ -57,7 +66,7 @@ def create_monitor_interface():
57
  "role": "user",
58
  "content": [
59
  {"type": "text", "text": prompt},
60
- {"type": "image", "image": img_byte_arr}
61
  ]
62
  }
63
  ],
@@ -78,14 +87,19 @@ def create_monitor_interface():
78
 
79
  # Add text overlay
80
  overlay = display_frame.copy()
81
- cv2.rectangle(overlay, (5, 5), (640, 200), (0, 0, 0), -1)
 
82
  cv2.addWeighted(overlay, 0.3, display_frame, 0.7, 0, display_frame)
83
 
 
84
  y_position = 30
85
- for line in analysis.split('\n'):
 
86
  cv2.putText(display_frame, line[:80], (10, y_position),
87
  cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
88
  y_position += 30
 
 
89
 
90
  return display_frame, analysis
91
 
@@ -94,7 +108,7 @@ def create_monitor_interface():
94
 
95
  with gr.Blocks() as demo:
96
  gr.Markdown("""
97
- # Real-time Safety Monitoring System
98
  Upload an image to analyze workplace safety concerns.
99
  """)
100
 
@@ -102,13 +116,16 @@ def create_monitor_interface():
102
  input_image = gr.Image(label="Upload Image")
103
  output_image = gr.Image(label="Analysis Results")
104
 
105
- analysis_text = gr.Textbox(label="Safety Analysis", lines=5)
106
 
107
  def analyze_image(image):
108
  if image is None:
109
  return None, "No image provided"
110
- processed_frame, analysis = monitor.process_frame(image)
111
- return processed_frame, analysis
 
 
 
112
 
113
  input_image.change(
114
  fn=analyze_image,
@@ -118,9 +135,9 @@ def create_monitor_interface():
118
 
119
  gr.Markdown("""
120
  ## Instructions:
121
- 1. Click the 'Upload Image' area or drag and drop an image
122
- 2. The system will automatically analyze the safety concerns
123
- 3. View the analyzed image and detailed safety report
124
  """)
125
 
126
  return demo
 
6
  from PIL import Image as PILImage
7
  import io
8
  import os
9
+ import base64
10
 
11
  def create_monitor_interface():
12
  api_key = os.getenv("GROQ_API_KEY")
 
39
  if frame is None:
40
  return "No frame received"
41
 
42
+ # Convert numpy array to PIL Image
43
+ if len(frame.shape) == 2: # If grayscale
44
+ frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
45
+ elif len(frame.shape) == 3 and frame.shape[2] == 4: # If RGBA
46
+ frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2RGB)
47
+
48
  frame_pil = PILImage.fromarray(frame)
49
+
50
+ # Convert to base64
51
+ buffered = io.BytesIO()
52
+ frame_pil.save(buffered, format="JPEG")
53
+ img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
54
 
55
  prompt = """Analyze this image for workplace safety issues. Focus on:
56
  1. PPE usage (helmets, vests, etc.)
 
66
  "role": "user",
67
  "content": [
68
  {"type": "text", "text": prompt},
69
+ {"type": "image", "image_url": f"data:image/jpeg;base64,{img_base64}"}
70
  ]
71
  }
72
  ],
 
87
 
88
  # Add text overlay
89
  overlay = display_frame.copy()
90
+ height, width = display_frame.shape[:2]
91
+ cv2.rectangle(overlay, (5, 5), (width-5, 100), (0, 0, 0), -1)
92
  cv2.addWeighted(overlay, 0.3, display_frame, 0.7, 0, display_frame)
93
 
94
+ # Add analysis text
95
  y_position = 30
96
+ lines = analysis.split('\n')
97
+ for line in lines:
98
  cv2.putText(display_frame, line[:80], (10, y_position),
99
  cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
100
  y_position += 30
101
+ if y_position >= 90: # Prevent text from going outside the overlay
102
+ break
103
 
104
  return display_frame, analysis
105
 
 
108
 
109
  with gr.Blocks() as demo:
110
  gr.Markdown("""
111
+ # Safety Monitoring System
112
  Upload an image to analyze workplace safety concerns.
113
  """)
114
 
 
116
  input_image = gr.Image(label="Upload Image")
117
  output_image = gr.Image(label="Analysis Results")
118
 
119
+ analysis_text = gr.Textbox(label="Detailed Safety Analysis", lines=5)
120
 
121
  def analyze_image(image):
122
  if image is None:
123
  return None, "No image provided"
124
+ try:
125
+ processed_frame, analysis = monitor.process_frame(image)
126
+ return processed_frame, analysis
127
+ except Exception as e:
128
+ return None, f"Error processing image: {str(e)}"
129
 
130
  input_image.change(
131
  fn=analyze_image,
 
135
 
136
  gr.Markdown("""
137
  ## Instructions:
138
+ 1. Upload an image using the input panel
139
+ 2. The system will automatically analyze it for safety concerns
140
+ 3. View the analyzed image with overlay and detailed analysis below
141
  """)
142
 
143
  return demo