I tried to make a color sorter with usb camera and raspberry pi 3 and I got as far as selecting an area in python with opencv but for some reason I can't retrieve the pixels of that area in an array.
import cv2
import time
import pandas as pd
import numpy as np
index = ["color", "color_name", "hex", "R", "G", "B"]
csv = pd.read_csv('colors.csv', names=index, header=None)
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 800)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 600)
def get_color_name(R, G, B):
minimum = 10000
for i in range(len(csv)):
d = abs(R - int(csv.loc[i, "R"])) + abs(G - int(csv.loc[i, "G"])) + abs(B - int(csv.loc[i, "B"]))
if d <= minimum:
minimum = d
cname = csv.loc[i, "color_name"]
return cname
while True:
_, frame = cap.read()
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
height, width, _ = frame.shape
zone = int(16)
y1 = int((width / 2)+(zone/2))
x2 = int((height / 2)+(zone/2))
x1 = int((width / 2)-(zone/2))
y2 = int((height / 2)-(zone/2))
selection = frame[y1:y2, x1:x2]
print(selection)
#cv2.circle(frame, (int(width / 2),int(height / 2)), 5, (255, 0, 0), 3)
cv2.rectangle(frame, (y1, y2), (x1, x2), (255, 0, 0), 2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
time.sleep(0.1)
cap.release()
cv2.destroyAllWindows()
Can anyone help?