Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,7 @@ from groq import Groq
|
|
8 |
import logging
|
9 |
import cv2
|
10 |
import numpy as np
|
|
|
11 |
|
12 |
# Set up logging
|
13 |
logging.basicConfig(level=logging.DEBUG)
|
@@ -41,7 +42,33 @@ def encode_image(image):
|
|
41 |
logger.error(f"Error encoding image: {str(e)}")
|
42 |
raise
|
43 |
|
44 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
if not images and video is None:
|
46 |
logger.warning("No images or video provided")
|
47 |
return [("No input", "Error: Please upload images or a video for analysis.")]
|
@@ -53,7 +80,8 @@ def analyze_construction_image(images, video=None):
|
|
53 |
if images:
|
54 |
for i, image_file in enumerate(images):
|
55 |
image = Image.open(image_file.name)
|
56 |
-
|
|
|
57 |
messages = [
|
58 |
{
|
59 |
"role": "user",
|
@@ -84,54 +112,44 @@ def analyze_construction_image(images, video=None):
|
|
84 |
results.append((f"Image {i+1} analysis", result))
|
85 |
|
86 |
if video:
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
{
|
103 |
-
"type": "text",
|
104 |
-
"text": f"Analyze this frame from a construction site video (Frame {i+1}/5 at {time_point*100}% of video duration). Identify any safety issues or hazards, categorize them, provide a detailed description, and suggest steps to resolve them."
|
105 |
-
},
|
106 |
-
{
|
107 |
-
"type": "image_url",
|
108 |
-
"image_url": {
|
109 |
-
"url": image_data_url
|
110 |
-
}
|
111 |
}
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
|
128 |
logger.info("Analysis completed successfully")
|
129 |
return results
|
130 |
except Exception as e:
|
131 |
logger.error(f"Error during analysis: {str(e)}")
|
132 |
-
logger.error(traceback.format_exc())
|
133 |
-
|
134 |
-
return [("Analysis error", error_message)]
|
135 |
|
136 |
def chat_about_image(message, chat_history):
|
137 |
try:
|
|
|
8 |
import logging
|
9 |
import cv2
|
10 |
import numpy as np
|
11 |
+
import traceback
|
12 |
|
13 |
# Set up logging
|
14 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
42 |
logger.error(f"Error encoding image: {str(e)}")
|
43 |
raise
|
44 |
|
45 |
+
def resize_image(image, max_size=(800, 800)):
|
46 |
+
"""Resize image to avoid exceeding the API size limits."""
|
47 |
+
try:
|
48 |
+
image.thumbnail(max_size, Image.ANTIALIAS)
|
49 |
+
return image
|
50 |
+
except Exception as e:
|
51 |
+
logger.error(f"Error resizing image: {str(e)}")
|
52 |
+
raise
|
53 |
+
|
54 |
+
def extract_frames_from_video(video, frame_points=[0, 0.5, 1], max_size=(800, 800)):
|
55 |
+
"""Extract key frames from the video at specific time points."""
|
56 |
+
cap = cv2.VideoCapture(video)
|
57 |
+
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
58 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
59 |
+
duration = frame_count / fps
|
60 |
+
|
61 |
+
frames = []
|
62 |
+
for time_point in frame_points:
|
63 |
+
cap.set(cv2.CAP_PROP_POS_MSEC, time_point * duration * 1000)
|
64 |
+
ret, frame = cap.read()
|
65 |
+
if ret:
|
66 |
+
resized_frame = cv2.resize(frame, max_size)
|
67 |
+
frames.append(resized_frame)
|
68 |
+
cap.release()
|
69 |
+
return frames
|
70 |
+
|
71 |
+
def analyze_construction_image(images=None, video=None):
|
72 |
if not images and video is None:
|
73 |
logger.warning("No images or video provided")
|
74 |
return [("No input", "Error: Please upload images or a video for analysis.")]
|
|
|
80 |
if images:
|
81 |
for i, image_file in enumerate(images):
|
82 |
image = Image.open(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",
|
|
|
112 |
results.append((f"Image {i+1} analysis", result))
|
113 |
|
114 |
if video:
|
115 |
+
frames = extract_frames_from_video(video.name) # Extract fewer frames from 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 construction site video (Frame {i+1}/5). Identify any safety issues or hazards, categorize them, provide a detailed description, and suggest steps to resolve them."
|
125 |
+
},
|
126 |
+
{
|
127 |
+
"type": "image_url",
|
128 |
+
"image_url": {
|
129 |
+
"url": image_data_url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
}
|
131 |
+
}
|
132 |
+
]
|
133 |
+
}
|
134 |
+
]
|
135 |
+
completion = client.chat.completions.create(
|
136 |
+
model="llama-3.2-90b-vision-preview",
|
137 |
+
messages=messages,
|
138 |
+
temperature=0.7,
|
139 |
+
max_tokens=1000,
|
140 |
+
top_p=1,
|
141 |
+
stream=False,
|
142 |
+
stop=None
|
143 |
+
)
|
144 |
+
result = completion.choices[0].message.content
|
145 |
+
results.append((f"Video frame {i+1} analysis", result))
|
146 |
|
147 |
logger.info("Analysis completed successfully")
|
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):
|
155 |
try:
|