Open In App
Related Articles

Automating Scrolling using Python-Opencv by Color Detection

Improve Article
Save Article
Like Article


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.


  • 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 frame in a variable
  • Get every color of the frame.
  • 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 frame using cv2.imshow()and pass the frame name and the frame variable to show every captured frame, put the frame 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.


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, frame =
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, low_green, high_green)
    contours, hierarchy = cv2.findContours(
    for i in contours:
        area = cv2.contourArea(i)
        if area > 1000:
            x, y, w, h = cv2.boundingRect(i)
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
            if y < prev_y:
            prev_y = y
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):


Detecting green color

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 03 Jan, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials