Я хочу использовать массив значений кадра, который включает в себя цикл видео, но я не могу получить значения из функции обработки видео. То, что я хочу сделать, это взять значения последовательности кадров из функции видео, а затем использовать эти значения в основной функции. Но и область цикла while, и область функции видео не позволяют этого. Я был бы признателен, если бы вы могли помочь. Спасибо вам отныне.
def video_processing():
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
while True:
ret, frame = cap.read()
if ret == True :
cv2.imshow("Image", frame)
if cv2.waitKey(1) & 0xFF == 27 :
cap.release()
break
def main():
video_processing()
print frame #is not working!
if __name__ == '__main__':
main()
Обновление (полный код)
import Tkinter as tk
import ttk
from ttk import Frame
import os
import time
from Tkinter import *
from Tkinter import Tk, Label
import cv2
import threading
import time
import imutils
import numpy as np
import matplotlib.pyplot as plt
from PIL import ImageTk, Image
from threading import Thread
Frames = []
def video_processing():
cap = cv2.VideoCapture(0)
# cap = cv2.VideoCapture("C:\Users\eren\OneDrive\Desktop\WIN_20190522_18_16_29_Pro.mp4")
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240)
global Frames
while True:
global Frames
ret, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
y , x = hsv.shape[:2] #x = 320 , y = 240
# Define 'brown' range in HSV colorspace
lower = np.array([10, 100, 20])
upper = np.array([20, 255, 200])
# Threshold the HSV image to get only brown color
mask1 = cv2.inRange(hsv, lower, upper)
kernel = np.ones((5,5),np.uint8)
thresh = cv2.dilate(mask1,kernel,iterations = 2)
# find contours in thresholded image, then grab the largest
# one
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
# determine the most extreme points along the contour
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
cv2.drawContours(thresh, [c], -1, (0, 255, 255), 2)
cv2.circle(thresh, extLeft , 8, (0, 0, 255) , -1)
cv2.circle(thresh, extRight, 8, (0, 255, 0) , -1)
cv2.circle(thresh, extTop , 8, (255, 0, 0) , -1)
cv2.circle(thresh, extBot , 8, (255, 255, 0), -1)
x_center = (extLeft[0] + extRight[0] + extTop[0] + extBot[0])/4
y_center = (extLeft[1] + extRight[1] + extTop[1] + extBot[1])/4
cv2.circle(frame,(x_center, y_center), 3, (0,255,0), -1)
cv2.line(frame,(extLeft[0] ,0),(extLeft[0],y) ,(0,255,0),2) # y axis - binary
cv2.line(frame,(extRight[0],0),(extRight[0],y),(0,255,0),2) # y axis - binary
cv2.line(frame,(0,extTop[1]) ,(x,extTop[1]) ,(0,255,0),2) # x axis - binary
cv2.line(frame,(0,extBot[1]) ,(x,extBot[1]) ,(0,255,0),2) # x axis - binary
# cv2.imshow("mask" , thresh)
cv2.imshow("Image", frame)
Frames = frame
# print frame
# return frame
if cv2.waitKey(1) & 0xFF == 27 :
cap.release()
break
# return frame
def main():
global Frames
video_processing()
print Frames
# print a
if __name__ == '__main__':
main()