Kushalmanda commited on
Commit
970cc25
·
verified ·
1 Parent(s): c1ee055

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -1,15 +1,15 @@
1
  import torch
2
- from transformers import AutoFeatureExtractor, AutoModelForObjectDetection
3
- from PIL import Image
4
  import gradio as gr
 
 
5
  import numpy as np
 
6
 
7
  # Load the pre-trained DETR model and feature extractor
8
  model = AutoModelForObjectDetection.from_pretrained("facebook/detr-resnet-50")
9
  extractor = AutoFeatureExtractor.from_pretrained("facebook/detr-resnet-50")
10
 
11
  # Simplified COCO label mapping (DETR uses COCO dataset)
12
- # In a real scenario, you'd need a custom model trained for construction labels
13
  COCO_LABELS = {56: "wall", 60: "foundation"} # Pretending chair (56) is "wall", dining table (60) is "foundation"
14
 
15
  # Function to calculate materials based on detected areas
@@ -39,13 +39,20 @@ def calculate_materials(detected_objects, image_width, image_height):
39
 
40
  return materials
41
 
42
- # Define the function for image inference
43
- def predict_image(image):
44
- # Convert Gradio image (numpy array) to PIL Image
45
- if isinstance(image, np.ndarray):
46
- image = Image.fromarray(image)
47
 
48
- # Prepare image for the model
 
 
 
 
 
 
 
 
49
  inputs = extractor(images=image, return_tensors="pt")
50
 
51
  # Run inference with DETR
@@ -59,7 +66,6 @@ def predict_image(image):
59
  for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
60
  box = box.tolist()
61
  label_id = label.item()
62
- # Map the label to a construction-related name (simplified for demo)
63
  label_name = COCO_LABELS.get(label_id, "unknown")
64
  if label_name != "unknown": # Only process relevant objects
65
  detected_objects.append({
@@ -83,12 +89,12 @@ def predict_image(image):
83
  # Set up Gradio interface
84
  interface = gr.Interface(
85
  fn=predict_image,
86
- inputs=gr.Image(type="numpy", label="Upload Blueprint Image"),
87
  outputs=gr.JSON(label="Material Estimates"),
88
  title="Blueprint Material Estimator",
89
- description="Upload a blueprint image to estimate construction materials."
90
  )
91
 
92
  # Launch the interface
93
  if __name__ == "__main__":
94
- interface.launch(share=False)
 
1
  import torch
 
 
2
  import gradio as gr
3
+ from PIL import Image
4
+ from pdf2image import convert_from_path
5
  import numpy as np
6
+ from transformers import AutoFeatureExtractor, AutoModelForObjectDetection
7
 
8
  # Load the pre-trained DETR model and feature extractor
9
  model = AutoModelForObjectDetection.from_pretrained("facebook/detr-resnet-50")
10
  extractor = AutoFeatureExtractor.from_pretrained("facebook/detr-resnet-50")
11
 
12
  # Simplified COCO label mapping (DETR uses COCO dataset)
 
13
  COCO_LABELS = {56: "wall", 60: "foundation"} # Pretending chair (56) is "wall", dining table (60) is "foundation"
14
 
15
  # Function to calculate materials based on detected areas
 
39
 
40
  return materials
41
 
42
+ # Function to process PDFs and convert to images
43
+ def pdf_to_image(pdf_file):
44
+ images = convert_from_path(pdf_file, first_page=1, last_page=1) # Convert the first page of the PDF
45
+ return images[0] # Return the first page as an image
 
46
 
47
+ # Define the function for image inference
48
+ def predict_image(file):
49
+ # Check if the input file is a PDF or image
50
+ if isinstance(file, str) and file.endswith('.pdf'):
51
+ image = pdf_to_image(file) # Convert PDF to image
52
+ else:
53
+ image = Image.open(file) # Open the image file
54
+
55
+ # Convert the image to the correct format for the model
56
  inputs = extractor(images=image, return_tensors="pt")
57
 
58
  # Run inference with DETR
 
66
  for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
67
  box = box.tolist()
68
  label_id = label.item()
 
69
  label_name = COCO_LABELS.get(label_id, "unknown")
70
  if label_name != "unknown": # Only process relevant objects
71
  detected_objects.append({
 
89
  # Set up Gradio interface
90
  interface = gr.Interface(
91
  fn=predict_image,
92
+ inputs=gr.File(label="Upload Blueprint (PDF or Image)"),
93
  outputs=gr.JSON(label="Material Estimates"),
94
  title="Blueprint Material Estimator",
95
+ description="Upload a blueprint image or PDF to estimate construction materials."
96
  )
97
 
98
  # Launch the interface
99
  if __name__ == "__main__":
100
+ interface.launch(share=False)