Skip to content

Instantly share code, notes, and snippets.

@venetanji
Created December 10, 2023 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save venetanji/15c7c60c81b825f071633bac58625643 to your computer and use it in GitHub Desktop.
Save venetanji/15c7c60c81b825f071633bac58625643 to your computer and use it in GitHub Desktop.
import cv2
import asyncio
import time
# open camera and get video capture object
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# set camera resolution
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
size=10
cap.set(cv2.CAP_PROP_BUFFERSIZE,size)
# set fps
cap.set(cv2.CAP_PROP_FPS, 60)
background_tasks = set()
async def show_cam():
# loop until 'q' pressed
global options
options = dict({'detecting': False})
def detect(frame):
time.sleep(1)
print("detecting", options['detecting'])
options['detecting'] = False
print("detecting value in detect after", options['detecting'])
while True:
# read frame
ret, frame = cap.read()
# rotate 90 degrees
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
#frame = cv2.flip(frame, 1)
# convert to grayscale
print("detecting value", options['detecting'])
if not options['detecting']:
print("not detecting")
options['detecting'] = True
asyncio.get_running_loop().run_in_executor(None, detect, frame)
# show frame
cv2.imshow('frame', frame)
# check for 'q' to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#rendermain = asyncio.create_task(show_cam())
asyncio.run(show_cam())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment