using open cv to control relay

asked 2020-10-27 09:49:25 -0500

Noob87 gravatar image

Hi all,

First off, I'm new to the forum so I wanted to say hi.

Also, as you can tell from my name, I am very new to all this, but I'm eager to learn, so any help would be much appreciated!

Ok, so I want to use open cv to be able to identify 3 colors: red blue, and green. and then when it detects those colors, sends a signal to the GPIO pins to open and close a relay.

I've played around with some code that I have found, but frankly, I'm lost.

Again, any help would be greatly appreciated!

edit retag flag offensive close merge delete


And your problem is...? What did you try? It seems that you didn't invest the minimal effort to solve this.

kbarni gravatar imagekbarni ( 2020-10-27 10:22:16 -0500 )edit

I'm lost as to how I should even start this project to be quite frank. I've been reading through all the tutorials I can find on open cv and it seems that everything is geared to capturing images and processing them, but not actually doing anything physical, if that makes sense. I want the program to be able to pick out either green, blue, or red colors. Then, once one of those colors is detected, send a signal to a relay to turn on a pump for a specified length of time. If you could just point me in the right direction I would greatly appreciate it!

Noob87 gravatar imageNoob87 ( 2020-10-27 16:21:22 -0500 )edit

I want to use open cv to be able to identify 3 colors: red blue, and green. and then when it detects those colors,

an image will be helpful to understand your need

sturkmen gravatar imagesturkmen ( 2020-10-27 17:04:12 -0500 )edit

@sturkmen. U don't needed images. Probably used webcam.

supra56 gravatar imagesupra56 ( 2020-10-28 07:42:03 -0500 )edit

@Noob87. What ur pi model OS? R u using webcam or picamera?

supra56 gravatar imagesupra56 ( 2020-10-28 07:43:57 -0500 )edit

I do not have relay switch. I can substitute for leds

supra56 gravatar imagesupra56 ( 2020-10-28 07:47:11 -0500 )edit

I'm sorry, I should have been more clear. I'm using a raspberry pi4 and a picamera. I have been able to alter code to pick up the three colors I need. I guess now my question is how do I get the program once it finds a color (say blue) to send a signal to the GPIO pins to turn on my relay for the pump? Once the program finds a color, is that stored as a variable which can be used to send such signal? I'm really confused. Thanks again everyone for your time.

Noob87 gravatar imageNoob87 ( 2020-10-28 07:54:32 -0500 )edit

I am using pi4 with 4gb/8gb, Buster v10. Thonny 3.7.3. I posted my answer detected blue colour. Notitced, I used 5% for 100 and 105. If ur colour is slightly than my. U can in increasing 10% to make sure rectangle doesn't shaking.. Btw, I do no have the code with me.I'm off for a years.

supra56 gravatar imagesupra56 ( 2020-10-28 09:11:16 -0500 )edit

With this:

if len(bluecnts)>0:
    blue_area = max(bluecnts, key=cv2.contourArea)
    (xg,yg,wg,hg) = cv2.boundingRect(blue_area)
    cv2.rectangle(frame,(xg,yg),(xg+wg, yg+hg),(0,255,0),2)
   #if rectangle is detected, then do something to set boolean to turn gpio on
supra56 gravatar imagesupra56 ( 2020-10-28 09:17:22 -0500 )edit

Btw, u can used VideoCapturer api with picamera

supra56 gravatar imagesupra56 ( 2020-10-28 09:22:03 -0500 )edit