Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,13 @@
|
|
1 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
from datetime import datetime
|
|
|
6 |
|
7 |
-
# Load
|
8 |
-
|
9 |
-
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
10 |
-
model.eval()
|
11 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
-
model.to(device)
|
13 |
|
14 |
-
#
|
15 |
-
def generate_captions_from_image(image):
|
16 |
-
if image.mode != "RGB":
|
17 |
-
image = image.convert("RGB")
|
18 |
-
|
19 |
-
# Preprocess the image and generate a caption
|
20 |
-
inputs = processor(image, return_tensors="pt").to(device, torch.float16)
|
21 |
-
output = model.generate(**inputs, max_new_tokens=50)
|
22 |
-
caption = processor.decode(output[0], skip_special_tokens=True)
|
23 |
-
|
24 |
-
return caption
|
25 |
-
|
26 |
-
# Function to generate the daily progress report (DPR) text
|
27 |
def generate_dpr(files):
|
28 |
dpr_text = []
|
29 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
@@ -34,18 +18,44 @@ def generate_dpr(files):
|
|
34 |
# Process each uploaded file (image)
|
35 |
for file in files:
|
36 |
# Open the image from file path
|
37 |
-
image = Image.open(file.name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
-
#
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
# Generate DPR section for this image with dynamic caption
|
46 |
-
dpr_section = f"\nImage: {file.name}\nDescription: {caption}\n"
|
47 |
dpr_text.append(dpr_section)
|
48 |
-
|
49 |
# Return the generated DPR as a text output
|
50 |
return "\n".join(dpr_text)
|
51 |
|
@@ -55,9 +65,8 @@ iface = gr.Interface(
|
|
55 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
56 |
outputs="text", # Display the DPR as text in the output section
|
57 |
title="Daily Progress Report Generator",
|
58 |
-
description="Upload up to 10 site photos. The AI model will
|
59 |
allow_flagging="never" # Optional: Disable flagging
|
60 |
)
|
61 |
|
62 |
iface.launch()
|
63 |
-
|
|
|
|
|
1 |
from PIL import Image
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from datetime import datetime
|
5 |
+
from ultralytics import YOLO
|
6 |
|
7 |
+
# Load YOLOv8 model (trained on construction dataset)
|
8 |
+
model = YOLO('yolov8n.pt') # Path to pre-trained model on construction dataset
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Function to generate DPR text based on detections
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def generate_dpr(files):
|
12 |
dpr_text = []
|
13 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
18 |
# Process each uploaded file (image)
|
19 |
for file in files:
|
20 |
# Open the image from file path
|
21 |
+
image = Image.open(file.name)
|
22 |
+
|
23 |
+
# Perform object detection with YOLOv8
|
24 |
+
results = model(image) # Perform detection
|
25 |
+
|
26 |
+
# Parse detections (activities, materials, etc.)
|
27 |
+
detected_objects = results.names # Object names detected by the model
|
28 |
+
detections = results.pandas().xywh # Get the dataframe with detection results
|
29 |
+
|
30 |
+
detected_activities = []
|
31 |
+
detected_materials = []
|
32 |
|
33 |
+
# Define construction activity and material categories
|
34 |
+
construction_activities = ['scaffolding', 'concrete pouring', 'welding', 'excavation']
|
35 |
+
construction_materials = ['concrete', 'steel', 'bricks', 'cement', 'sand']
|
36 |
|
37 |
+
# Check the detected objects and categorize them
|
38 |
+
for obj in detected_objects:
|
39 |
+
if obj.lower() in construction_activities:
|
40 |
+
detected_activities.append(obj)
|
41 |
+
elif obj.lower() in construction_materials:
|
42 |
+
detected_materials.append(obj)
|
43 |
+
|
44 |
+
# Build a detailed report for this image
|
45 |
+
dpr_section = f"\nImage: {file.name}\n"
|
46 |
+
|
47 |
+
if detected_activities:
|
48 |
+
dpr_section += f"Detected Activities: {', '.join(detected_activities)}\n"
|
49 |
+
else:
|
50 |
+
dpr_section += "No construction activities detected.\n"
|
51 |
+
|
52 |
+
if detected_materials:
|
53 |
+
dpr_section += f"Detected Materials: {', '.join(detected_materials)}\n"
|
54 |
+
else:
|
55 |
+
dpr_section += "No materials detected.\n"
|
56 |
|
|
|
|
|
57 |
dpr_text.append(dpr_section)
|
58 |
+
|
59 |
# Return the generated DPR as a text output
|
60 |
return "\n".join(dpr_text)
|
61 |
|
|
|
65 |
inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
|
66 |
outputs="text", # Display the DPR as text in the output section
|
67 |
title="Daily Progress Report Generator",
|
68 |
+
description="Upload up to 10 site photos. The AI model will detect construction activities, materials, and progress and generate a text-based Daily Progress Report (DPR).",
|
69 |
allow_flagging="never" # Optional: Disable flagging
|
70 |
)
|
71 |
|
72 |
iface.launch()
|
|