capradeepgujaran commited on
Commit
c3b34ed
1 Parent(s): 92928c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -66
app.py CHANGED
@@ -47,46 +47,6 @@ class SafetyMonitor:
47
  img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
48
  return f"data:image/jpeg;base64,{img_base64}"
49
 
50
- def get_scene_context(self, image):
51
- """Analyze the scene context."""
52
- try:
53
- image_url = self.encode_image(image)
54
- completion = self.client.chat.completions.create(
55
- model=self.model_name,
56
- messages=[
57
- {
58
- "role": "user",
59
- "content": [
60
- {
61
- "type": "text",
62
- "text": """Analyze this workplace image and identify key areas and elements. Include:
63
- 1. Worker locations and activities
64
- 2. Equipment and machinery
65
- 3. Materials and storage
66
- 4. Access routes and paths
67
- 5. Hazardous areas
68
-
69
- Format each observation as:
70
- - Element: specific location in image"""
71
- },
72
- {
73
- "type": "image_url",
74
- "image_url": {
75
- "url": image_url
76
- }
77
- }
78
- ]
79
- }
80
- ],
81
- temperature=0.3,
82
- max_tokens=200,
83
- stream=False
84
- )
85
- return completion.choices[0].message.content
86
- except Exception as e:
87
- print(f"Scene analysis error: {str(e)}")
88
- return ""
89
-
90
  def analyze_frame(self, frame):
91
  """Perform safety analysis on the frame."""
92
  if frame is None:
@@ -113,14 +73,6 @@ class SafetyMonitor:
113
  }
114
  }
115
  ]
116
- },
117
- {
118
- "role": "assistant",
119
- "content": "I'll analyze the image for safety hazards and provide the location and description of each concern."
120
- },
121
- {
122
- "role": "user",
123
- "content": "Please format each observation as: - <location>position:detailed safety concern</location>"
124
  }
125
  ],
126
  temperature=0.7,
@@ -136,12 +88,6 @@ class SafetyMonitor:
136
  """Convert textual position to coordinates."""
137
  height, width = image_shape[:2]
138
 
139
- # Parse position for spatial information
140
- position = position.lower()
141
-
142
- # Base coordinates (full image)
143
- x1, y1, x2, y2 = 0, 0, width, height
144
-
145
  # Define regions
146
  regions = {
147
  'center': (width//3, height//3, 2*width//3, 2*height//3),
@@ -158,18 +104,9 @@ class SafetyMonitor:
158
  'middle': (0, height//3, width, 2*height//3)
159
  }
160
 
161
- # Find best matching region
162
- best_match = None
163
- max_match = 0
164
- for region, coords in regions.items():
165
- if region in position:
166
- words = region.split('-')
167
- matches = sum(1 for word in words if word in position)
168
- if matches > max_match:
169
- max_match = matches
170
- best_match = coords
171
-
172
- return best_match if best_match else (x1, y1, x2, y2)
173
 
174
  def draw_observations(self, image, observations):
175
  """Draw bounding boxes and labels for safety observations."""
@@ -184,6 +121,7 @@ class SafetyMonitor:
184
 
185
  # Get coordinates for this observation
186
  x1, y1, x2, y2 = self.get_region_coordinates(obs['location'], image.shape)
 
187
 
188
  # Draw rectangle
189
  cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)
 
47
  img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
48
  return f"data:image/jpeg;base64,{img_base64}"
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def analyze_frame(self, frame):
51
  """Perform safety analysis on the frame."""
52
  if frame is None:
 
73
  }
74
  }
75
  ]
 
 
 
 
 
 
 
 
76
  }
77
  ],
78
  temperature=0.7,
 
88
  """Convert textual position to coordinates."""
89
  height, width = image_shape[:2]
90
 
 
 
 
 
 
 
91
  # Define regions
92
  regions = {
93
  'center': (width//3, height//3, 2*width//3, 2*height//3),
 
104
  'middle': (0, height//3, width, 2*height//3)
105
  }
106
 
107
+ # Ensure the region name from the model output matches one of our predefined regions
108
+ position = position.lower()
109
+ return regions.get(position, (0, 0, width, height)) # Default to full image if no match
 
 
 
 
 
 
 
 
 
110
 
111
  def draw_observations(self, image, observations):
112
  """Draw bounding boxes and labels for safety observations."""
 
121
 
122
  # Get coordinates for this observation
123
  x1, y1, x2, y2 = self.get_region_coordinates(obs['location'], image.shape)
124
+ print(f"Drawing box at coordinates: ({x1}, {y1}, {x2}, {y2}) for {obs['description']}")
125
 
126
  # Draw rectangle
127
  cv2.rectangle(image, (x1, y1), (x2, y2), color, 2)