DmitrMakeev commited on
Commit
d00009b
·
verified ·
1 Parent(s): e97c94d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -666,28 +666,25 @@ 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
- # Преобразуем в HSV
671
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
672
 
673
- # Диапазоны цветов в HSV
674
- green_mask = cv2.inRange(hsv, (36, 40, 40), (86, 255, 255))
675
- yellow_mask = cv2.inRange(hsv, (46, 40, 220), (56, 100, 255))
676
-
677
- brown_mask = cv2.inRange(hsv, (8, 50, 30), (18, 255, 130))
 
 
678
 
679
  total_pixels = img.shape[0] * img.shape[1]
 
680
 
681
- green_percent = int((cv2.countNonZero(green_mask) / total_pixels) * 100)
682
- yellow_percent = int((cv2.countNonZero(yellow_mask) / total_pixels) * 100)
683
- brown_percent = int((cv2.countNonZero(brown_mask) / total_pixels) * 100)
684
-
685
- return {
686
- "green": green_percent,
687
- "yellow": yellow_percent,
688
- "brown": brown_percent
689
- }
690
 
 
691
 
692
 
693
  @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
+ # Обновлённые диапазоны HSV (Hue, Saturation, Value)
672
+ color_ranges = {
673
+ "green": ((36, 40, 40), (86, 255, 255)), # Зелёный
674
+ "yellow": ((20, 100, 100), (30, 255, 255)), # Жёлтый
675
+ "brown": ((10, 50, 20), (20, 150, 150)), # Коричневый
676
+ "orange": ((5, 100, 100), (15, 255, 255)) # Оранжевый
677
+ }
678
 
679
  total_pixels = img.shape[0] * img.shape[1]
680
+ result = {}
681
 
682
+ for color, (lower, upper) in color_ranges.items():
683
+ mask = cv2.inRange(hsv, np.array(lower), np.array(upper))
684
+ percent = int((cv2.countNonZero(mask) / total_pixels) * 100)
685
+ result[color] = percent
 
 
 
 
 
686
 
687
+ return result
688
 
689
 
690
  @app.route('/last_image', methods=['GET'])