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

Yes, i'm using a code that does that - it is able to pick out red, blue, and green colors from a live stream via the picamera. now, I need it to send a signal to my relay whenever it sees those colors to open the relay to start a pump. that is the part that I'm stuck on.

Noob87 gravatar imageNoob87 ( 2020-10-28 10:52:15 -0500 )edit

Can u post snippet code(not whole code)? If it is detected colour, then set boolean 1. And then turned gpio pin(s) to relay switch on. U need 3 colour at simultaneously or one at a time? Can u post image of robot arms? U have one channel(one relay switch)?

supra56 gravatar imagesupra56 ( 2020-10-28 11:03:26 -0500 )edit

Here is example:

import RPi.GPIO as GPIO
import time

channel = 21

# GPIO setup
GPIO.setup(channel, GPIO.OUT)

def detect_blue(pin):
   #put blue code in here
    GPIO.output(pin, GPIO.HIGH)  # Turn motor on

def detect_blue_off(pin):
    GPIO.output(pin, GPIO.LOW)  # Turn motor off

if __name__ == '__main__':
    except KeyboardInterrupt:
supra56 gravatar imagesupra56 ( 2020-10-28 11:56:54 -0500 )edit

I can't post here because of max characters.

supra56 gravatar imagesupra56 ( 2020-10-28 21:58:47 -0500 )edit


import numpy as np 
import cv2 

# Capturing video through webcam 
webcam = cv2.VideoCapture(0)  
# Start a while loop 
    # Reading the video from the 
    # webcam in image frames 
    _, imageFrame = 

    # Convert the imageFrame in  
    # BGR(RGB color space) to  
    # HSV(hue-saturation-value) 
    # color space 
    hsvFrame = cv2.cvtColor(imageFrame, cv2.COLOR_BGR2HSV) 

    # Set range for red color and  
    # define mask 
    red_lower = np.array([136, 87, 111], np.uint8) 
    red_upper = np.array([180, 255, 255], np.uint8) 
    red_mask = cv2.inRange(hsvFrame, red_lower, red_upper) 

    # Set range for green color and  
    # define mask 
    green_lower = np.array([25, 52, 72], np.uint8) 
    green_upper = np.array([102, 255
Noob87 gravatar imageNoob87 ( 2020-10-28 23:32:23 -0500 )edit

@ Supra56, thank you for all your help - I hope this helps. Would it be ok to reach you via email?

Noob87 gravatar imageNoob87 ( 2020-10-28 23:33:22 -0500 )edit