DmitrMakeev commited on
Commit
5410b0b
·
verified ·
1 Parent(s): f97c494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -4
app.py CHANGED
@@ -666,13 +666,17 @@ def analyze_colors(image_bytes):
666
  image_bytes.seek(0)
667
  file_bytes = np.asarray(bytearray(image_bytes.read()), dtype=np.uint8)
668
  img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
 
 
 
669
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
670
 
 
671
  color_ranges = {
672
- "green": ((36, 40, 40), (86, 255, 255)),
673
- "yellow": ((46, 40, 220), (56, 100, 255)),
674
- "orange": ((10, 150, 150), (20, 255, 255)),
675
- "brown": ((5, 50, 20), (15, 150, 150))
676
  }
677
 
678
  total_pixels = img.shape[0] * img.shape[1]
@@ -680,9 +684,19 @@ def analyze_colors(image_bytes):
680
 
681
  for color, (lower, upper) in color_ranges.items():
682
  mask = cv2.inRange(hsv, np.array(lower), np.array(upper))
 
 
 
 
 
683
  percent = round(cv2.countNonZero(mask) / total_pixels * 100, 1)
684
  results[color] = percent
685
 
 
 
 
 
 
686
  return results
687
 
688
  @app.route('/last_image', methods=['GET'])
 
666
  image_bytes.seek(0)
667
  file_bytes = np.asarray(bytearray(image_bytes.read()), dtype=np.uint8)
668
  img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
669
+
670
+ # Предварительная обработка изображения
671
+ img = cv2.GaussianBlur(img, (3, 3), 0) # Уменьшаем шумы
672
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
673
 
674
+ # Оптимизированные диапазоны HSV
675
  color_ranges = {
676
+ "green": ((35, 50, 50), (85, 255, 255)), # Зеленый
677
+ "yellow": ((22, 100, 150), (32, 255, 255)), # Желтый (расширенный диапазон)
678
+ "orange": ((10, 150, 150), (20, 255, 255)), # Оранжевый
679
+ "brown": ((5, 50, 30), (15, 180, 180)) # Коричневый (более темный)
680
  }
681
 
682
  total_pixels = img.shape[0] * img.shape[1]
 
684
 
685
  for color, (lower, upper) in color_ranges.items():
686
  mask = cv2.inRange(hsv, np.array(lower), np.array(upper))
687
+
688
+ # Улучшаем маску морфологическими операциями
689
+ kernel = np.ones((3,3), np.uint8)
690
+ mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
691
+
692
  percent = round(cv2.countNonZero(mask) / total_pixels * 100, 1)
693
  results[color] = percent
694
 
695
+ # Для отладки можно сохранять маски
696
+ if DEBUG_MODE:
697
+ for color, mask in masks.items():
698
+ cv2.imwrite(f'{color}_mask.jpg', mask)
699
+
700
  return results
701
 
702
  @app.route('/last_image', methods=['GET'])