import numpy as np
import argparse
import cv2

# color detection

# input picture
image = cv2.imread('image-12-22-38.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)


# setting up color ranges 

# green color
low_orange = np.array([10, 127, 144])
high_orange = np.array([255, 255, 243])
green_mask = cv2.inRange(image_hsv, low_orange, high_orange)

# orange color
low_orange = np.array([0, 100, 180])
high_orange = np.array([14, 255, 255])
orange_mask = cv2.inRange(image_hsv, low_orange, high_orange)

# white color
low_white = np.array([10, 88, 144])
high_white = np.array([20, 255, 255])
white_mask = cv2.inRange(image_hsv, low_white, high_white)

# black color
low_black = np.array([0, 0, 0])
high_black = np.array([255, 226, 70])
black_mask = cv2.inRange(image_hsv, low_black, high_black)

# gray color
low_gray = np.array([0, 45, 106])
high_gray = np.array([120, 255, 255])
gray_mask = cv2.inRange(image_hsv, low_gray, high_gray)


# creation of a mask
mask = [orange_mask, gray_mask, white_mask, green_mask, black_mask]
c_t  = ['orange', 'gray', 'white','green', 'black']


result_c = 0
for i in range(0, len(mask)):
	contours=cv2.findContours(mask[i],cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[-2]

	# looping over the contours
	for pic, contour in enumerate(contours):
		area = cv2.contourArea(contour)
		#print(area)
		if area>2500 and area<8500:
			color_rpi = c_t[i]
			part_type_rpi = i+1
			print(color_rpi) 
			#print(color,part_type)
			x,y,w,h = cv2.boundingRect(contour)	
			cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
			result_c = 1

	if result_c == 1:
		break
		
cv2.imshow("image",image)

cv2.waitKey(0)
cv2.destroyAllWindows()
