Home I have two OpenCV programs that work well separately, but I tried combining them and it doesn't work
Reply: 0

I have two OpenCV programs that work well separately, but I tried combining them and it doesn't work

Gouri Parvathy Thampi
1#
Gouri Parvathy Thampi Published in 2018-02-13 12:27:06Z

First program : is to move mouse cursor with an object. In this program you enter the RGB color code of the object to be detected and the sensitivity (just enter 20).

Second program : is to implement mouse clicks with the user's blinks

So basically what i want is one combined program where I move mouse cursor with and object and implement clicking with eye blinks. Every time I combine the program, it crashes and I can't figure out what to do.

First program

 import numpy as np
 import win32api, win32con
 import cv2
 import operator

 mouseAcc = 10

 def bgrToHSV( bgr ):
     return cv2.cvtColor(np.uint8([[bgr]]), cv2.COLOR_BGR2HSV)  

 def getBoundsForBGR( bgr, tolerance ):
     hsv = bgrToHSV(bgr)[0][0]  
     return (np.array([max((0, hsv[0] - tolerance)), 100, 100]), 
 np.array([min(255, hsv[0] + tolerance), 255, 255]))    

 def clamp(min_v, max_v, val):
     return max(min(max_v, val), min_v)


 print ("To exit, press ESC\n\n")
 print ("Enter the color of the object you want to follow (B,R,G):")
 colourToTrack = [clamp(0, 255, int(input("Blue (0-255): "))), clamp(0, 255, 
 int(input("Green (0-255): "))), clamp(0, 255, int(input("Red (0-255): ")))]
 treshold = int(clamp(5, 50, int(input("Sensitivity (5-50): "))))

 colourBounds = getBoundsForBGR(colourToTrack, treshold)
 kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (20, 20))

 camera = cv2.VideoCapture(0)
 prevBoxPos = None

 while(1):

     cursorPos = win32api.GetCursorPos()
     trackObjDelta = (0, 0)

     _, frame = camera.read()
    frame = cv2.flip(frame, 1)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

     mask = cv2.inRange(hsv, colourBounds[0], colourBounds[1])  
     mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)      

     image,contours,hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

     if len(contours) > 0:
        rects = map(lambda c : cv2.minAreaRect(c), contours)
        rect = max(rects, key=lambda r : r[1][0] * r[1][1])         
        box = cv2.boxPoints(rect)
        box = np.int0(box)
        cv2.drawContours(frame,[box],0,(0,0,255),2)

         boxPos = (int(rect[0][0]), int(rect[0][1]))        
         cv2.circle(frame, boxPos, 5, (255, 0, 0), 10)

         if prevBoxPos != None:
            delta = ((boxPos[0] - prevBoxPos[0]) * mouseAcc, (boxPos[1] - 
 prevBoxPos[1]) * mouseAcc)
            win32api.SetCursorPos(tuple(map(operator.add, cursorPos, delta)))           
         prevBoxPos = boxPos

     cv2.imshow("camera", frame)        

     k = cv2.waitKey(5) & 0xFF
     if k == 27:
        break

 cv2.destroyAllWindows()

Second program

import numpy as np
import cv2
import pyautogui as m
import sys
import time

k = 1
user_input = 1

face_cascade = cv2.CascadeClassifier('C:/Users/hp/Desktop/project/haarcascades/haarcascade_frontalface_default.xml')#Replace this with the path where you reside the file
eye_cascade = cv2.CascadeClassifier('C:/Users/hp/Desktop/project/haarcascades/haarcascade_eye.xml')#Replace this with the path where you reside the file
cap = cv2.VideoCapture(0)
if user_input == 1:
    while (True):   

        ret, img = cap.read()
        img = cv2.flip(img, 1)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            k = 1
            img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
            u1 = x + w
            u2 = y + h
            cir = cv2.circle(img, (int(x + w / 2), int(y + h / 2)), 1, (0, 255, 255), 2)

            roi_gray = gray[y:y + h, x:x + w]
            roi_color = img[y:y + h, x:x + w]
            eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
            for (ex, ey, ew, eh) in eyes:
                k = 0
                cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (255, 255, 0), 2)

        if k == 1:
            print ("Blink")
            # The below snippet is responsible for mouse click events. At the moment this is would work but still needs enhancements
            # uncomment the below to enable mouse left click events.
            m.click()
        if k == 0:
            print ("No Blink")
        #cv2.namedWindow("img", cv2.WINDOW_NORMAL)
        #cv2.resizeWindow("img", 600, 450)
        #cv2.imshow('img', img)
        #if cv2.waitKey(1) & 0xff == ord('q'):
        #    break
   # cap.release()
   # cv2.destroyAllWindows()
else:
    print ("wrong key ")
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308243 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO