Rekham1110 commited on
Commit
6864c26
·
verified ·
1 Parent(s): 25656fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -9
app.py CHANGED
@@ -8,6 +8,7 @@ import shutil
8
  import base64
9
  import pytz
10
  import hashlib
 
11
 
12
  # Load environment variables
13
  load_dotenv()
@@ -43,22 +44,23 @@ def analyze_image_with_hf(image_path):
43
  img = Image.open(image_path)
44
  detected_elements = []
45
 
46
- # Enhanced mock detection based on image size and pixel data (simulating content analysis)
47
  width, height = img.size
48
- img_data = list(img.getdata())
49
- gray_pixels = sum(1 for pixel in img_data if sum(pixel[:3]) / 3 < 128) / len(img_data) # Proportion of dark pixels
50
  total_pixels = width * height
 
 
 
51
 
52
  # Heuristic detection based on image characteristics
53
- if total_pixels < 500000: # Small image size, assume early planning stage
54
  detected_elements.append("planning")
55
- elif gray_pixels > 0.6 and width > 1000: # High proportion of dark pixels, assume concrete/foundation
56
  detected_elements.append("foundation")
57
- elif gray_pixels > 0.4 and width > 1000 and height > 1000: # Larger structure with columns, assume walls erected
58
  detected_elements.append("walls")
59
- elif any(keyword in os.path.basename(image_path).lower() for keyword in ["interior", "plumbing", "electrical", "ceiling"]) or (gray_pixels < 0.3 and total_pixels > 800000): # Interior work or bright finished interior
60
  detected_elements.extend(["interior", "electrical_wiring", "plumbing_pipes"])
61
- else: # Assume completed if no other clear indicators
62
  detected_elements.extend(["roof", "windows", "doors", "cladding", "finished"])
63
 
64
  return detected_elements
@@ -180,7 +182,7 @@ def process_image(images, project_name):
180
  final_milestone = "Completed"
181
  percent_complete = 100
182
  else:
183
- completed_tasks.extend(all_tasks["Planning"]) # Default to Planning if no specific stage detected
184
  final_milestone = "Planning"
185
  percent_complete = 10
186
 
 
8
  import base64
9
  import pytz
10
  import hashlib
11
+ import statistics
12
 
13
  # Load environment variables
14
  load_dotenv()
 
44
  img = Image.open(image_path)
45
  detected_elements = []
46
 
47
+ # Enhanced mock detection based on image size, pixel data, and color variance
48
  width, height = img.size
 
 
49
  total_pixels = width * height
50
+ img_data = list(img.convert('RGB').getdata())
51
+ gray_pixels = sum(1 for pixel in img_data if sum(pixel[:3]) / 3 < 128) / len(img_data) # Proportion of dark pixels
52
+ color_variance = statistics.variance([sum(pixel[:3]) / 3 for pixel in img_data]) if len(img_data) > 1 else 0 # Variance in brightness
53
 
54
  # Heuristic detection based on image characteristics
55
+ if total_pixels < 300000 or color_variance < 1000: # Small size or low color variance, assume planning/design
56
  detected_elements.append("planning")
57
+ elif gray_pixels > 0.5 and total_pixels > 500000: # High dark pixels and moderate size, assume foundation
58
  detected_elements.append("foundation")
59
+ elif gray_pixels > 0.3 and total_pixels > 1000000 and color_variance > 2000: # Larger size with moderate dark pixels and high variance, assume walls
60
  detected_elements.append("walls")
61
+ elif gray_pixels < 0.2 or any(keyword in os.path.basename(image_path).lower() for keyword in ["interior", "plumbing", "electrical", "ceiling"]): # Bright image or interior keywords
62
  detected_elements.extend(["interior", "electrical_wiring", "plumbing_pipes"])
63
+ else: # Balanced colors and large size, assume completed
64
  detected_elements.extend(["roof", "windows", "doors", "cladding", "finished"])
65
 
66
  return detected_elements
 
182
  final_milestone = "Completed"
183
  percent_complete = 100
184
  else:
185
+ completed_tasks.extend(all_tasks["Planning"]) # Fallback to Planning if no clear stage
186
  final_milestone = "Planning"
187
  percent_complete = 10
188