Motor driving using template matching using python and opencv on Rapberry pi

2018-06-23 12:06:52

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.rectang