开放式ROI的计时器计数器

2024-09-27 07:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我在计算一个感兴趣区域在屏幕上停留的总时间。你知道吗

事实上,我正在用我的摄像头追踪一个insetc,我需要知道它在左侧和右侧停留了多少次。我已经在追踪昆虫方面取得了成功,但是当我尝试在代码中输入定时器计数时,它会在同一时间启动许多定时器。你知道吗

`

import cv2
import numpy as np
import argparse
import datetime
import imutils
import time
import datetime
import threading
from multiprocessing import Pool
import Temporizador
import Temporizador2

# analizador de argumentos
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
ap.add_argument("-a", "--min-area", type=int, default=2000, help="minimum area size")
args = vars(ap.parse_args())

if args.get("video", None) is None:
    cap = cv2.VideoCapture(1)
    time.sleep(0.01)

else:
    cap = cv2.VideoCapture(args["video"])

# Primeiro quadro

firstframe = None

while True:

    # threading._start_new_thread(Temporizador.teste, (15, "Vanzoca esta aqui"))
    # Take each frame
    grabbed, frame = cap.read()

    if not grabbed:
        break

    cinza = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # convert to grayscale

    suavizado = cv2.GaussianBlur(cinza, (7, 7), 0)  # blur

    borda = cv2.Canny(suavizado, 100, 200)  # get Canny edges#######################naum tem no exemplo

    if firstframe is None:
        firstframe = suavizado
        continue

    frameDelta = cv2.absdiff(firstframe, suavizado)
    tresh = cv2.threshold(frameDelta, 135, 255, cv2.THRESH_BINARY)[
        1]  # Exibe o em preto (255) tudo que passa do limite (2a variavel) *ajuste de acordo com a luminozidade (105 p noite e 125 p dia)

    # dilata o limiar da imagem ate q ele complete os buracos e encontre os contornos
    tresh = cv2.dilate(tresh, None, iterations=2)
    (leonardo, cnts, _) = cv2.findContours(tresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    ########################################tirei o loop para quadrados mto pequenos linha 60 do cara
    # loop over the contours

    for C in cnts:
        # Ignora o contorno caso seja mto pequeno - tratamento de erros
        if cv2.contourArea(C) < args["min_area"]:
            continue

        # gera a caixa da area de interesse e desenha o quadro no frame e atualiza o texto
        (x, y, w, h) = cv2.boundingRect(C)
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 1)

        # dicionando o texto de tempo

        # draw the text and timestamp on the frame
        text = "Recording Crockroaches"
        cv2.putText(frame, "{}".format(text), (10, 18),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        cv2.putText(frame, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"),
                    (10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)

        # Encontrando o centro da barata

        x1 = w / 2
        y1 = h / 2

        cx = x + x1
        cy = y + y1

        # Usa os valores do centro da barata pra centralizar o ponto de referencia
        cv2.circle(frame, (int(cx), int(cy)), 2, (0, 0, 255), -1)

        # Criando um vetor que carrega a posicao x e y do centro da barata
        centroid = (cx, cy)

        cv2.putText(frame, "Crockroache", (cx + w / 2, cy - h / 2), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 25, 255), 1,
                    cv2.LINE_AA)

        # Verifica e cria o texto indicando se a barata esta a direita ou a esquerda da linha central

        direita = (320, 480) < centroid < (640, 480)

        # A Direita
        if direita:
            cv2.putText(frame, datetime.datetime.now().strftime(("Right")), (cx + w / 2, cy + h / 2),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.35, (200, 255, 25), 1,
                        cv2.LINE_AA)



            # A Esquerda
        if (0, 480) < centroid < (320, 480):
            cv2.putText(frame, datetime.datetime.now().strftime(("Left")), (cx + w / 2, cy + h / 2),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.35, (200, 255, 25), 1,
                        cv2.LINE_AA)

            # threading._start_new_thread(Temporizador2.teste, (12, "ESQUERDA"))

            ##Contador de tempo chamado via thread
            # p = Pool(1)
            # threading._start_new_thread(Temporizador.teste, (15, "Tempo na Esquerda:"))
            # print(p.map(Temporizador.teste, 12, "Tempo filmando:"))

            # print(p.map(Temporizador.teste(16,"Tempo esq") ))

        ####Linhas de Referencia - Linha do centro e Retrangulo ao redor do quadro da camera
        cv2.line(frame, (320, 0), (320, 480), (255, 0, 0), 1)
        cv2.rectangle(frame, (0, 0), (640, 480), (0, 255, 0), 5)

    # Exibindo os frames
    cv2.imshow("Frama Delta", frameDelta)
    cv2.imshow("Original", frame)
    cv2.imshow("Tresh", tresh)

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

cap.release()
cv2.destroyAllWindows()

`

有人能帮帮我吗?你知道吗

Crockroachs are tracked and i know if it`s in right or left, i need to count hoy many time in each side


Tags: importnonedatetimeifargsdecv2do

热门问题