Rammohan0504 commited on
Commit
df97270
·
verified ·
1 Parent(s): 0f2887d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -40
app.py CHANGED
@@ -1,15 +1,29 @@
 
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
- torch.serialization.add_safe_globals([torch.nn.Module, 'ultralytics.nn.tasks.DetectionModel'])
 
 
 
 
 
8
 
9
- # Load YOLOv8 model (trained on construction dataset)
10
- model = YOLO('yolov8n.pt') # Path to pre-trained model on construction dataset
 
 
 
 
 
 
 
 
 
11
 
12
- # Function to generate DPR text based on detections
13
  def generate_dpr(files):
14
  dpr_text = []
15
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
@@ -20,44 +34,18 @@ def generate_dpr(files):
20
  # Process each uploaded file (image)
21
  for file in files:
22
  # Open the image from file path
23
- image = Image.open(file.name)
24
-
25
- # Perform object detection with YOLOv8
26
- results = model(image) # Perform detection
27
-
28
- # Parse detections (activities, materials, etc.)
29
- detected_objects = results.names # Object names detected by the model
30
- detections = results.pandas().xywh # Get the dataframe with detection results
31
-
32
- detected_activities = []
33
- detected_materials = []
34
 
35
- # Define construction activity and material categories
36
- construction_activities = ['scaffolding', 'concrete pouring', 'welding', 'excavation']
37
- construction_materials = ['concrete', 'steel', 'bricks', 'cement', 'sand']
38
 
39
- # Check the detected objects and categorize them
40
- for obj in detected_objects:
41
- if obj.lower() in construction_activities:
42
- detected_activities.append(obj)
43
- elif obj.lower() in construction_materials:
44
- detected_materials.append(obj)
45
-
46
- # Build a detailed report for this image
47
- dpr_section = f"\nImage: {file.name}\n"
48
-
49
- if detected_activities:
50
- dpr_section += f"Detected Activities: {', '.join(detected_activities)}\n"
51
- else:
52
- dpr_section += "No construction activities detected.\n"
53
-
54
- if detected_materials:
55
- dpr_section += f"Detected Materials: {', '.join(detected_materials)}\n"
56
- else:
57
- dpr_section += "No materials detected.\n"
58
 
 
 
59
  dpr_text.append(dpr_section)
60
-
61
  # Return the generated DPR as a text output
62
  return "\n".join(dpr_text)
63
 
@@ -67,7 +55,7 @@ iface = gr.Interface(
67
  inputs=gr.Files(type="filepath", label="Upload Site Photos"), # Handle batch upload of images
68
  outputs="text", # Display the DPR as text in the output section
69
  title="Daily Progress Report Generator",
70
- 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).",
71
  allow_flagging="never" # Optional: Disable flagging
72
  )
73
 
 
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 BLIP model and processor
8
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
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
+ # Inference function to generate captions dynamically based on image content
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
  # Process each uploaded file (image)
35
  for file in files:
36
  # Open the image from file path
37
+ image = Image.open(file.name) # Using file.name for filepath
 
 
 
 
 
 
 
 
 
 
38
 
39
+ if image.mode != "RGB":
40
+ image = image.convert("RGB")
 
41
 
42
+ # Dynamically generate a caption based on the image
43
+ caption = generate_captions_from_image(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  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 dynamically detect construction activities, materials, and progress and generate a text-based Daily Progress Report (DPR).",
59
  allow_flagging="never" # Optional: Disable flagging
60
  )
61