capradeepgujaran commited on
Commit
0d34f3e
1 Parent(s): fe52e0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -79,16 +79,20 @@ def analyze_construction_image(images=None, video=None):
79
 
80
  if images:
81
  for i, image_file in enumerate(images):
82
- image = Image.open(image_file.name) # For image uploads, we use image_file.name
83
- resized_image = resize_image(image) # Resize image before processing
84
  image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
85
  messages = [
 
 
 
 
86
  {
87
  "role": "user",
88
  "content": [
89
  {
90
  "type": "text",
91
- "text": f"Analyze this construction site image (Image {i+1}/{len(images)}). Explain the image in detail. Identify any safety issues or hazards, categorize them, provide a detailed description, and suggest steps to resolve them."
92
  },
93
  {
94
  "type": "image_url",
@@ -112,16 +116,20 @@ def analyze_construction_image(images=None, video=None):
112
  results.append((f"Image {i+1} analysis", result))
113
 
114
  if video:
115
- frames = extract_frames_from_video(video) # Use video directly, as it's a file path
116
  for i, frame in enumerate(frames):
117
  image_data_url = f"data:image/png;base64,{encode_image(frame)}"
118
  messages = [
 
 
 
 
119
  {
120
  "role": "user",
121
  "content": [
122
  {
123
  "type": "text",
124
- "text": f"Analyze this frame from a construction site video (Frame {i+1}/5). Explain what you observed in the video. Identify any safety issues or hazards, categorize them, provide a detailed description, and suggest steps to resolve them."
125
  },
126
  {
127
  "type": "image_url",
@@ -148,7 +156,7 @@ def analyze_construction_image(images=None, video=None):
148
  return results
149
  except Exception as e:
150
  logger.error(f"Error during analysis: {str(e)}")
151
- logger.error(traceback.format_exc()) # Log the full traceback for debugging
152
  return [("Analysis error", f"Error during analysis: {str(e)}")]
153
 
154
  def chat_about_image(message, chat_history):
 
79
 
80
  if images:
81
  for i, image_file in enumerate(images):
82
+ image = Image.open(image_file.name)
83
+ resized_image = resize_image(image)
84
  image_data_url = f"data:image/png;base64,{encode_image(resized_image)}"
85
  messages = [
86
+ {
87
+ "role": "system",
88
+ "content": "You are an AI assistant specialized in analyzing images for safety issues. Your task is to first determine if the image shows a construction site. If it does, identify any safety issues or hazards, categorize them, provide a detailed description, and suggest steps to resolve them. If it's not a construction site, simply state that and briefly describe what you see in the image."
89
+ },
90
  {
91
  "role": "user",
92
  "content": [
93
  {
94
  "type": "text",
95
+ "text": f"Analyze this image (Image {i+1}/{len(images)}). First, determine if it's a construction site. If it is, explain the image in detail, focusing on safety aspects. If it's not, briefly describe what you see."
96
  },
97
  {
98
  "type": "image_url",
 
116
  results.append((f"Image {i+1} analysis", result))
117
 
118
  if video:
119
+ frames = extract_frames_from_video(video)
120
  for i, frame in enumerate(frames):
121
  image_data_url = f"data:image/png;base64,{encode_image(frame)}"
122
  messages = [
123
+ {
124
+ "role": "system",
125
+ "content": "You are an AI assistant specialized in analyzing video frames for safety issues. Your task is to first determine if the frame shows a construction site. If it does, identify any safety issues or hazards, categorize them, provide a detailed description, and suggest steps to resolve them. If it's not a construction site, simply state that and briefly describe what you see in the frame."
126
+ },
127
  {
128
  "role": "user",
129
  "content": [
130
  {
131
  "type": "text",
132
+ "text": f"Analyze this frame from a video (Frame {i+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."
133
  },
134
  {
135
  "type": "image_url",
 
156
  return results
157
  except Exception as e:
158
  logger.error(f"Error during analysis: {str(e)}")
159
+ logger.error(traceback.format_exc())
160
  return [("Analysis error", f"Error during analysis: {str(e)}")]
161
 
162
  def chat_about_image(message, chat_history):