pelaafv
Posts: 1
Joined: Tue Mar 03, 2020 3:27 pm

Detection of green stripes on products

Tue Mar 03, 2020 3:38 pm

Hello!

I am doing my final project related to the improvement of a production line of a painting utensils factory.

One of the improvements consists of detecting the defective rollers (which are previously marked with a green tape) and expelling them from the line by means of a piston. I'm new to the world of raspberry, but I've been told it's best to do it with Raspberry Pi Camera. I bought the Raspberry, the camera and everything else needed for it, I also installed Open CV.

My intention is that the Raspberry will give me a digital "1" if it detects the green stripe on the roller, amplify it to 24V and send the signal to the PLC to trigger a piston.

Does anyone know how the program will have to be for the Raspberry to detect that green stripe and give me a high signal?

Thank you very much,

Greetings!

User avatar
B.Goode
Posts: 10191
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Detection of green stripes on products

Tue Mar 03, 2020 5:16 pm

pelaafv wrote:
Tue Mar 03, 2020 3:38 pm
Hello!

I am doing my final project related to the improvement of a production line of a painting utensils factory.

One of the improvements consists of detecting the defective rollers (which are previously marked with a green tape) and expelling them from the line by means of a piston. I'm new to the world of raspberry, but I've been told it's best to do it with Raspberry Pi Camera. I bought the Raspberry, the camera and everything else needed for it, I also installed Open CV.

My intention is that the Raspberry will give me a digital "1" if it detects the green stripe on the roller, amplify it to 24V and send the signal to the PLC to trigger a piston.

Does anyone know how the program will have to be for the Raspberry to detect that green stripe and give me a high signal?

Thank you very much,

Greetings!

One way (not the only or best way) of approaching a programming problem is to imagine you already have an endlessly clever general purpose machine that already knows how to handle your problem.

So you might write - example not necessarily valid in any specific computer language -

Code: Select all

if green_stripe_detected
    trigger_piston

Then you continually refine and expand your code to handle green_stripe_detected and trigger_piston, and so on, until you have a working solution.


Transforming a 3.3 volt output from the gpio pin on the RPi to the 24 volts needed to actuate the piston will require external electronics.


Volunteer helpers here will probably help solve particular problems with your code once you have written some.

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: Detection of green stripes on products

Tue Mar 03, 2020 6:24 pm

Take a look at opencv and color filtering

Eg https://www.geeksforgeeks.org/filter-color-with-opencv/

Can you upload a picture?
Last edited by gordon77 on Tue Mar 03, 2020 9:03 pm, edited 1 time in total.

boyoh
Posts: 1468
Joined: Fri Nov 23, 2012 3:30 pm
Location: Selby. North Yorkshire .UK

Re: Detection of green stripes on products

Tue Mar 03, 2020 9:00 pm

If this is a commercial project on a production line , It might be better to let a commercial designee company do it for you

There might safety problems to write in the program , Such as fail to safe on a moving conveyor belt .

Regards BoyOh
BoyOh ( Selby, North Yorkshire.UK)
Some Times Right Some Times Wrong

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: Detection of green stripes on products

Wed Mar 04, 2020 1:48 pm

This will filter the green and count the detected pixels, if lower than a set threshold will light a led, or drive a relay etc, through the gpio specified. You need to investigate the interface to 24v.

You may also need to workout when / how to trigger the camera when the roller is in the required position.

Adjust the sliders to set the colour.

Code: Select all

#!/usr/bin/env python3
import cv2
import numpy as np
import RPi.GPIO as GPIO

# set threshold (no of pixels detected)
threshold = 6000

# setup GPIO outputs
out_gpio = 16
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup (out_gpio,GPIO.OUT)
GPIO.output(out_gpio,GPIO.LOW)

font = cv2.FONT_HERSHEY_SIMPLEX

def callback(x):
    pass

cap = cv2.VideoCapture(0)
cv2.namedWindow('image')

# startup values
lowH = 54
highH = 96
lowS = 52
highS = 255
lowV = 54
highV = 160

# create trackbars for color change
cv2.createTrackbar('lowH','image',lowH,255,callback)
cv2.createTrackbar('highH','image',highH,200,callback)

cv2.createTrackbar('lowS','image',lowS,255,callback)
cv2.createTrackbar('highS','image',highS,255,callback)

cv2.createTrackbar('lowV','image',lowV,255,callback)
cv2.createTrackbar('highV','image',highV,255,callback)

while True:
    # grab the frame
    ret, frame = cap.read()

    # get trackbar positions
    lowH = cv2.getTrackbarPos('lowH', 'image')
    highH = cv2.getTrackbarPos('highH', 'image')
    lowS = cv2.getTrackbarPos('lowS', 'image')
    highS = cv2.getTrackbarPos('highS', 'image')
    lowV = cv2.getTrackbarPos('lowV', 'image')
    highV = cv2.getTrackbarPos('highV', 'image')

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower_hsv = np.array([lowH, lowS, lowV])
    higher_hsv = np.array([highH, highS, highV])
    mask = cv2.inRange(hsv, lower_hsv, higher_hsv)
    frame2 = cv2.bitwise_and(frame, frame, mask=mask)

    gray_frame = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
    
    # convert gray_frame to 0/1s
    gray_frame[gray_frame <  50] = 0
    gray_frame[gray_frame >= 50] = 1
    
    # sum number of pixels
    total = np.sum(gray_frame)
    
    # trigger the output
    if total < threshold:
        GPIO.output(out_gpio,GPIO.HIGH)
        cv2.putText(frame2,str(total), (10, 15), font, .5, (0, 0, 255), 1)
    else:
        GPIO.output(out_gpio,GPIO.LOW)
        cv2.putText(frame2,str(total), (10, 15), font, .5, (0, 255, 0), 1)
        
    # show the images    
    dim = (320, 240)
    resize1 = cv2.resize(frame, dim, interpolation = cv2.INTER_AREA)
    resize2 = cv2.resize(frame2, dim, interpolation = cv2.INTER_AREA)
    output = np.hstack((resize1,resize2))
    cv2.imshow('image', output)
    
    k = cv2.waitKey(10) 
    # press esc to quit
    if k == 27:
        break
cv2.destroyAllWindows()
cap.release()
Attachments
green_filter.jpg
green_filter.jpg (29.23 KiB) Viewed 229 times
Last edited by gordon77 on Thu Mar 05, 2020 12:06 pm, edited 8 times in total.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26442
Joined: Sat Jul 30, 2011 7:41 pm

Re: Detection of green stripes on products

Wed Mar 04, 2020 2:00 pm

boyoh wrote:
Tue Mar 03, 2020 9:00 pm
If this is a commercial project on a production line , It might be better to let a commercial designee company do it for you

There might safety problems to write in the program , Such as fail to safe on a moving conveyor belt .

Regards BoyOh
Final year CS projects often have students talking to local companies to implement various production improvements or similar. So using a third party company to do the work is not possible - after all, this is the students learning! Also note that rarely do these projects ever get used 'live', but it does happen.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

User avatar
bensimmo
Posts: 4577
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Detection of green stripes on products

Wed Mar 04, 2020 2:15 pm

You don't amplify the Pi signal.
You use a GPIO output to signal to something else (on/off) and act like a switch.
That piston and everything about it will be self contained unit. You just switch it on to kick.

(Off course further feedback systems can be made, like jamming, has it kicked it, etc. But that's not part of your brief).

So all you need to worry about here is detecting green.
Send single to kicking unit.
Job done, given the above code, using opencv. The send signal should be easy. (Set a gpio high or low).

If you need to interface to the kicking machine, you need to know more about that, for now just turn an led on to demonstrate a signal.

I.e. does it really need at 24V pulse to trigger it?
If so you are over to external electronics.

Return to “Beginners”