How can I run a function every specific time within a while cycle on python?

Alex Ortega

I'm working in a project with OCR using a webcam. I would like to know how can I run a function every 3 seconds (for example) while the principal code still running. The code is very long. So I use the following to exemplify:

import cv2

cap = cv2.VideoCapture(0)

def capture():
    cv2.imwrite("frame.jpg", frame)

while(1):
    ret, frame = cap.read()
    cv2.imshow('Webcam', frame)
    (_, cnt, _) = cv2.findContours(frame, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    if len(cnt) > 10:
        # here I want to call capture() function every 3 seconds

    if cv2.waitKey(1) & 0xFF == ord('x'):
        break

cap.release()
cv2.destroyAllWindows()

I used time.sleep(3) after the statments of capture() function but it pause the continuous capture of the frame. I don't want it. I need something like a software interruption. When the flag shoots, the code of function run but the while cycle continues working.

I'm using python 2.7 on Windows 7.

I hope you understand my purpose. I rode about daemon and threads but I can't interpret so much.

Joshi Sravan Kumar

You can use it like this

import cv2
import threading
import time

cap=cv2.VideoCapture(0)

main_loop_running = True
def capture():
    global main_loop_running, frame
    while (main_loop_running):
        cv2.imwrite("frame.jpg", frame)
        time.sleep(3)

ret, frame = cap.read()
cv2.imshow('Webcam', frame)
child_t = threading.Thread(target=capture)
child_t.setDaemon(True)
child_t.start()

while(1):
    ret, frame = cap.read()
    cv2.imshow('Webcam', frame)

    # here I want to call capture() function every 3 seconds

    if cv2.waitKey(1) & 0xFF == ord('x'):
        break

main_loop_running = False
child_t.join()

cap.release()
cv2.destroyAllWindows()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Openerp - How can i call a function to run every time a specific view is called?

From Dev

In JavaScript, how can I have a function run at a specific time?

From Dev

How can I run Python commands every time I start Python

From Dev

How can I shutdown the system every day at a specific time

From Dev

My matplotlib bar-chart duplicates every time a function is run, how can I refresh my bar-chart?

From Dev

Running a function every time specific function type is run

From Dev

How can I make the server check a site every time? How can I run a script on a server?

From Dev

How can I run onclick function on python

From Dev

how do I create a function within a function to cycle?

From Dev

How to increase a number every time a function is run?

From Java

How can I run a python script from within Flask

From Dev

How can I run a python script from within Flask

From Dev

How can I make my function run in constant time?

From Dev

How can I run function for each element in different time?

From Dev

How I can calling a function at specific time in tornado

From Dev

How can I call function every 30 seconds on every devices in the same time?

From Dev

don't want to write query every time while i want to calculation, how can i use variable?

From Dev

How can I make GNU screen run a command or script every time I create a new window?

From Dev

How can I make GNU screen run a command or script every time I create a new window?

From Dev

How can I create a new file every time I run the program using Java

From Dev

How can I make a Python function examine every element of a list?

From Dev

How to run a function at specific time & date?

From Dev

How to run a function at a specific time in iOS?

From Dev

saving the number into the variable in every run of cycle python

From Dev

How can I run Sublime Text from the launcher as root every time?

From Dev

How can I run Sublime Text from the launcher as root every time?

From Dev

How can I run a script every time a certain GNU screen changes?

From Dev

How can I run a Bash script every time is plugged an USB device?

From Dev

How do I run a script every time "yum update" is run?

Related Related

  1. 1

    Openerp - How can i call a function to run every time a specific view is called?

  2. 2

    In JavaScript, how can I have a function run at a specific time?

  3. 3

    How can I run Python commands every time I start Python

  4. 4

    How can I shutdown the system every day at a specific time

  5. 5

    My matplotlib bar-chart duplicates every time a function is run, how can I refresh my bar-chart?

  6. 6

    Running a function every time specific function type is run

  7. 7

    How can I make the server check a site every time? How can I run a script on a server?

  8. 8

    How can I run onclick function on python

  9. 9

    how do I create a function within a function to cycle?

  10. 10

    How to increase a number every time a function is run?

  11. 11

    How can I run a python script from within Flask

  12. 12

    How can I run a python script from within Flask

  13. 13

    How can I make my function run in constant time?

  14. 14

    How can I run function for each element in different time?

  15. 15

    How I can calling a function at specific time in tornado

  16. 16

    How can I call function every 30 seconds on every devices in the same time?

  17. 17

    don't want to write query every time while i want to calculation, how can i use variable?

  18. 18

    How can I make GNU screen run a command or script every time I create a new window?

  19. 19

    How can I make GNU screen run a command or script every time I create a new window?

  20. 20

    How can I create a new file every time I run the program using Java

  21. 21

    How can I make a Python function examine every element of a list?

  22. 22

    How to run a function at specific time & date?

  23. 23

    How to run a function at a specific time in iOS?

  24. 24

    saving the number into the variable in every run of cycle python

  25. 25

    How can I run Sublime Text from the launcher as root every time?

  26. 26

    How can I run Sublime Text from the launcher as root every time?

  27. 27

    How can I run a script every time a certain GNU screen changes?

  28. 28

    How can I run a Bash script every time is plugged an USB device?

  29. 29

    How do I run a script every time "yum update" is run?

HotTag

Archive