0

I want to drive a motor when my template is matched, I have written code of template matching but can't figure out how to drive the motor with it.

This is my code:

import cv2
import numpy as np
import matplotlib.pyplot as plt
import RPi.GPIO as GPIO   # Import the GPIO library.
import time               # Import time library

GPIO.setmode(GPIO.BOARD)

GPIO.setup(10, GPIO.OUT)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)  # Set GPIO pin 12 to output mode.
pwm = GPIO.PWM(12, 100)

pwm.start(0)

cap = cv2.VideoCapture(0) #Webcam Capture

while(True):

    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)



    template = cv2.imread('gate.jpg',0)
    w, h = template.shape[::-1]

    res = cv2.matchTemplate(gray,template,cv2.TM_CCOEFF_NORMED)
    threshold = 0.8
    loc = np.where(res>threshold)

    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    top_left = min_loc
    bottom_right = (top_left[0]+w,top_left[1]+h)
    cv2.rectangle(frame,top_left, bottom_right, 255, 1)
    cv2.putText(frame, 'Detected Face ID: ',         (top_left[0],top_left[1]-10), 
        cv2.FONT_HERSHEY_PLAIN, 1.0, (255,255,255))


    cv2.imshow('Test',frame)

    lower_blue = np.array([100,10,10])
    upper_blue = np.array([255,100,100])
    lower_red = np.array([10,10,100])
    upper_red = np.array([100,100,255])
    lower_green = np.array([10,100,10])
    upper_green = np.array([100,255,100])

    mask_blue = cv2.inRange(frame, lower_blue, upper_blue)
    mask_red = cv2.inRange(frame, lower_red, upper_red)
    mask_green = cv2.inRange(frame, lower_green, upper_green)

# Bitwise-AND mask and original image
#    res_blue = cv2.bitwise_and(frame,frame, mask_blue= mask_blue)
#    res_red = cv2.bitwise_and(frame,frame, mask_red= mask_red)
#    res_green = cv2.bitwise_and(frame,frame, mask_green= mask_green)

    cv2.imshow('Blue',mask_blue)
    cv2.imshow('Red',mask_red)
    cv2.imshow('Green',mask_green)

#    cv2.imshow('res',res)    

    if():
        GPIO.output(8,GPIO.HIGH)
        GPIO.output(10,GPIO.LOW)
        pwm.ChangeDutyCycle(100)
    else:
        pwm.ChangeDutyCycle(0)


    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
GPIO.cleanup()
cap.release()
cv2.destroyAllWindows() 

I have defined Threshold value using this

threshold = 0.8
loc = np.where(res>threshold)

Then i am trying this command to derive my motors when template is mated

if(loc):
   GPIO.output(8,GPIO.HIGH)
   GPIO.output(10,GPIO.LOW)
   pwm.ChangeDutyCycle(100)
else:
   pwm.ChangeDutyCycle(0)

when i use "if(loc)" motor starts even if template is not matched when i use "loc==True" motor does not start even when template is matched Can anyone help me???

  • What data type is loc? I think if loc will always be true as long as loc is not zero. if loc == True will always be false unless loc is 1 or True. – joan Jun 23 '18 at 9:34
  • So what should I do?? – Gnahy Jun 24 '18 at 10:33
  • I would write a tiny program which just sets and tests loc. Just 4 or 5 lines. That should show if what I suggest is the problem or not. – joan Jun 24 '18 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.