capradeepgujaran
commited on
Commit
•
0d34f3e
1
Parent(s):
fe52e0c
Update app.py
Browse files
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)
|
83 |
-
resized_image = resize_image(image)
|
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
|
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)
|
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
|
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())
|
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):
|