DmitrMakeev commited on
Commit
d407ea7
·
verified ·
1 Parent(s): d57c72c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -27
app.py CHANGED
@@ -666,43 +666,23 @@ 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
-
670
- # Масштабируем изображение для ускорения обработки (опционально)
671
- img = cv2.resize(img, (0,0), fx=0.5, fy=0.5)
672
-
673
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
674
-
675
- # Точные диапазоны HSV (подбираются экспериментально)
676
  color_ranges = {
677
- "green": ((35, 40, 40), (85, 255, 255)), # Зеленый
678
- "yellow": ((22, 100, 100), (32, 255, 255)), # Желтый
679
- "orange": ((10, 150, 150), (20, 255, 255)), # Оранжевый
680
- "brown": ((5, 50, 20), (15, 150, 150)) # Коричневый
681
  }
682
 
683
- # Создаем debug-изображение
684
- debug_img = img.copy()
685
  total_pixels = img.shape[0] * img.shape[1]
686
  results = {}
687
 
688
  for color, (lower, upper) in color_ranges.items():
689
  mask = cv2.inRange(hsv, np.array(lower), np.array(upper))
690
-
691
- # Улучшаем маску морфологическими операциями
692
- kernel = np.ones((5,5), np.uint8)
693
- mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
694
- mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
695
-
696
- # Добавляем на debug-изображение
697
- debug_img = cv2.addWeighted(debug_img, 0.7,
698
- cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR), 0.3, 0)
699
-
700
- percent = round((cv2.countNonZero(mask) / total_pixels * 100, 1)
701
  results[color] = percent
702
-
703
- # Сохраняем debug-изображение для анализа
704
- cv2.imwrite('debug_colors.jpg', debug_img)
705
-
706
  return results
707
 
708
  @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
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
670
+
 
671
  color_ranges = {
672
+ "green": ((36, 40, 40), (86, 255, 255)),
673
+ "yellow": ((22, 100, 100), (32, 255, 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]
679
  results = {}
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'])