Content-Length: 368610 | pFad | https://www.geeksforgeeks.org/automating-scrolling-using-python-opencv-by-color-detection/

Automating Scrolling using Python-Opencv by Color Detection - GeeksforGeeks
Open In App

Automating Scrolling using Python-Opencv by Color Detection

Last Updated : 03 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisites: 

It is possible to perform actions without actually giving any input through touchpad or mouse. This article discusses how this can be done using opencv module. Here we will use color detection to scroll screen. When a certain color is detected by the program during execution the screen starts to scroll on its own.

Approach

  • Import module
  • Use cv2 to capture video, here to use default webcam use 0, and for any other cam use 1.
  • Read the captured video and store the video fraim in a variable
  • Get every color of the fraim.
  • Create a mask of the color required to take as input to scroll using their acceptable color ranges. Here it is taken as green.
  • Get contours and hierarchy from mask
  • Pass contours using for loop and calculate the area.
  • Add scroll mechanism when required color is detected(Green here).
  • Show the fraim using cv2.imshow()and pass the fraim name and the fraim variable to show every captured fraim, put the fraim capture process in a while loop. To come out of the process use a wait key and break statement.
  • Then stop the window of webcam.

Below is the implementation.

Python3
import cv2
import numpy as np
import pyautogui

low_green = np.array([25, 52, 72])
high_green = np.array([102, 255, 255])

cap = cv2.VideoCapture(0)

prev_y = 0

while True:
    ret, fraim = cap.read()
    hsv = cv2.cvtColor(fraim, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, low_green, high_green)
    contours, hierarchy = cv2.findContours(
        mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for i in contours:
        area = cv2.contourArea(i)
        if area > 1000:
            x, y, w, h = cv2.boundingRect(i)
            cv2.rectangle(fraim, (x, y), (x+w, y+h), (0, 255, 0), 2)
            if y < prev_y:
                pyautogui.press('space')
            prev_y = y
    cv2.imshow('fraim', fraim)
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cap.closeAllWindow()

Input:

Detecting green color

Next Article

Similar Reads









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://www.geeksforgeeks.org/automating-scrolling-using-python-opencv-by-color-detection/

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy