capradeepgujaran commited on
Commit
63e45ea
·
verified ·
1 Parent(s): f95de19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -10
app.py CHANGED
@@ -99,12 +99,8 @@ def analyze_construction_media(media):
99
  if file_type in ['jpg', 'jpeg', 'png', 'gif']:
100
  # Handle image
101
  try:
102
- with Image.open(file_path) as image:
103
- logger.info(f"Successfully opened image: {file_path}")
104
- resized_image = resize_image(image)
105
- image_data = encode_image(resized_image)
106
- logger.info(f"Image {i+1} encoded, size: {len(image_data)} bytes")
107
- image_data_url = f"data:image/png;base64,{image_data}"
108
 
109
  messages = [
110
  {
@@ -117,7 +113,7 @@ def analyze_construction_media(media):
117
  {
118
  "type": "image_url",
119
  "image_url": {
120
- "url": image_data_url
121
  }
122
  }
123
  ]
@@ -144,9 +140,48 @@ def analyze_construction_media(media):
144
  results.append((f"Image {i+1} analysis", f"Error processing image: {str(img_error)}"))
145
 
146
  elif file_type in ['mp4', 'avi', 'mov', 'wmv']:
147
- # Handle video (keep the existing video processing code)
148
- ...
149
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  else:
151
  logger.warning(f"Unsupported file type: {file_type}")
152
  results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))
 
99
  if file_type in ['jpg', 'jpeg', 'png', 'gif']:
100
  # Handle image
101
  try:
102
+ image_base64 = encode_image_to_base64(file_path)
103
+ logger.info(f"Image {i+1} encoded, size: {len(image_base64)} bytes")
 
 
 
 
104
 
105
  messages = [
106
  {
 
113
  {
114
  "type": "image_url",
115
  "image_url": {
116
+ "url": f"data:image/{file_type};base64,{image_base64}"
117
  }
118
  }
119
  ]
 
140
  results.append((f"Image {i+1} analysis", f"Error processing image: {str(img_error)}"))
141
 
142
  elif file_type in ['mp4', 'avi', 'mov', 'wmv']:
143
+ # Handle video
144
+ try:
145
+ frames = extract_frames_from_video(file_path)
146
+ logger.info(f"Extracted {len(frames)} frames from video: {file_path}")
147
+ for j, frame in enumerate(frames):
148
+ frame_base64 = encode_image_to_base64(frame)
149
+ logger.info(f"Video {i+1}, Frame {j+1} encoded, size: {len(frame_base64)} bytes")
150
+
151
+ messages = [
152
+ {
153
+ "role": "user",
154
+ "content": [
155
+ {
156
+ "type": "text",
157
+ "text": f"{instruction}\n\nAnalyze this frame from a video (File {i+1}/{len(media)}, Frame {j+1}/{len(frames)}). First, determine if it's a construction site. If it is, explain what you observe, focusing on safety aspects. If it's not, briefly describe what you see."
158
+ },
159
+ {
160
+ "type": "image_url",
161
+ "image_url": {
162
+ "url": f"data:image/png;base64,{frame_base64}"
163
+ }
164
+ }
165
+ ]
166
+ }
167
+ ]
168
+ completion = client.chat.completions.create(
169
+ model="llama-3.2-90b-vision-preview",
170
+ messages=messages,
171
+ temperature=0.7,
172
+ max_tokens=1000,
173
+ top_p=1,
174
+ stream=False,
175
+ stop=None
176
+ )
177
+ result = completion.choices[0].message.content
178
+ results.append((f"Video {i+1}, Frame {j+1} analysis", result))
179
+ logger.info(f"Successfully analyzed video {i+1}")
180
+ except Exception as vid_error:
181
+ logger.error(f"Error processing video {i+1}: {str(vid_error)}")
182
+ logger.error(traceback.format_exc())
183
+ results.append((f"Video {i+1} analysis", f"Error processing video: {str(vid_error)}"))
184
+
185
  else:
186
  logger.warning(f"Unsupported file type: {file_type}")
187
  results.append((f"File {i+1} analysis", f"Unsupported file type: {file_type}"))