[PYTHON] Hinweise für herausfordernde Basketball-Videoanalysen

http://qiita.com/northriver/items/d6b73da79a13bf3526e2 Nachdem Sie sich mit opencv auskennen, fordern wir den Titel heraus

Verstehen Sie zunächst, etwas zu verwenden, das hilfreich zu sein scheint

Ich habe einen Code gefunden, der den grünen Ball verfolgt, also werde ich es versuchen. Dies scheint an der Farbe zu erkennen http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/

#Importieren Sie die erforderlichen Pakete
from collections import deque
import numpy as np
import argparse
import imutils
import cv2
#Machen Sie ein Argument zur Ausführung. "Python xxx.py -v test.mov "-Über v
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=64,help="max buffer size")
args = vars(ap.parse_args())

Klicken Sie hier, um eine Erklärung zu argparse zu erhalten. Http://python.civic-apps.com/argparse/

#Definieren Sie die Farbe des Verfolgungsballs"green" (HSV)
greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)
pts = deque(maxlen=args["buffer"])

HSV-Farbraum http://www.peko-step.com/html/hsv.html

#Wenn es ein Argument gibt, den Pfad der Datei, falls nicht, zur Webcam
if not args.get("video", False):
	camera = cv2.VideoCapture(0)
else:
	camera = cv2.VideoCapture(args["video"])
while True:
	#Kamera, nimm ein Video auf
	(grabbed, frame) = camera.read()
 
	#Wenn Sie nicht können, brechen Sie
	if args.get("video") and not grabbed:
		break
 
	# resize
	frame = imutils.resize(frame, width=600)
	#In hsv konvertieren
	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
	#Extrahieren Sie nur den grünen Teil und führen Sie eine Morphologiekonvertierung durch
	mask = cv2.inRange(hsv, greenLower, greenUpper)
	mask = cv2.erode(mask, None, iterations=2)
	mask = cv2.dilate(mask, None, iterations=2)

Ich mache eine Morphologiekonvertierung. Die Morphologietransformation extrahiert den charakteristischen Teil des Objekts und entfernt die anderen Teile. Ich versuche es zu reduzieren. cv2.erode zieht sich zusammen (Erosion), um den charakteristischen Teil zu verlassen, und cv2.dilate erweitert sich (Dilatation), um nur den wichtigen Teil wiederherzustellen. http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_morphological_ops/py_morphological_ops.html

	#Extrahieren Sie die Kontur aus binären Schwarzweißdaten
	# (x, y) center of the ball
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
	center = None

Extrahieren Sie mit cv2.findContours Konturen aus binären Schwarzweißdaten. RETR_EXTERNAL ist ein Modus, der nur den äußersten Teil extrahiert. CHAIN_APPROX_SIMPLE ist eine Methode zur Konturnäherung http://opencv.jp/opencv-2.1/cpp/structural_analysis_and_shape_descriptors.html

    if len(cnts) > 0:
        #Berechnen Sie die Fläche, die von der Fläche eingenommen wird, und finden Sie die Fläche mit der größten Fläche
        c = max(cnts, key=cv2.contourArea)
        #Finde den kleinsten Kreis
        ((x, y), radius) = cv2.minEnclosingCircle(c)
    #Moment(Grundwerte wie Fläche und Schwerpunkt)Und erhalten Sie die Koordinaten des Mittelpunkts des Kreises
        M = cv2.moments(c)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

        #Zeichne einen Kreis
        if radius > 10:
            cv2.circle(frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
            cv2.circle(frame, center, 5, (0, 0, 255), -1)

http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html

Das Folgende ist, um der Flugbahn zu folgen, damit Sie nicht müssen

	# update the points queue
	pts.appendleft(center)
	# loop over the set of tracked points
	for i in xrange(1, len(pts)):
		# if either of the tracked points are None, ignore
		# them
		if pts[i - 1] is None or pts[i] is None:
			continue
 
		# otherwise, compute the thickness of the line and
		# draw the connecting lines
		thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5)
		cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness)

Der Rest wird nur ausgegeben, dies ist das gleiche wie im Tutorial

	# show the frame to our screen
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF
 
	# if the 'q' key is pressed, stop the loop
	if key == ord("q"):
		break
 
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()

Verbinden Sie sich von Anfang bis Ende

# import the necessary packages
from collections import deque
import numpy as np
import argparse
import imutils
import cv2
 
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
	help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=64,
	help="max buffer size")
args = vars(ap.parse_args())

# define the lower and upper boundaries of the "green"
# ball in the HSV color space, then initialize the
# list of tracked points
greenLower = (29, 86, 6)
greenUpper = (64, 255, 255)
pts = deque(maxlen=args["buffer"])
 
# if a video path was not supplied, grab the reference
# to the webcam
if not args.get("video", False):
	camera = cv2.VideoCapture(0)
 
# otherwise, grab a reference to the video file
else:
	camera = cv2.VideoCapture(args["video"])

# keep looping
while True:
	# grab the current frame
	(grabbed, frame) = camera.read()
 
	# if we are viewing a video and we did not grab a frame,
	# then we have reached the end of the video
	if args.get("video") and not grabbed:
		break
 
	# resize the frame, blur it, and convert it to the HSV
	# color space
	frame = imutils.resize(frame, width=600)
	# blurred = cv2.GaussianBlur(frame, (11, 11), 0)
	hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
 
	# construct a mask for the color "green", then perform
	# a series of dilations and erosions to remove any small
	# blobs left in the mask
	mask = cv2.inRange(hsv, greenLower, greenUpper)
	mask = cv2.erode(mask, None, iterations=2)
	mask = cv2.dilate(mask, None, iterations=2)
	# find contours in the mask and initialize the current
	# (x, y) center of the ball
	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
		cv2.CHAIN_APPROX_SIMPLE)[-2]
	center = None
 
	# only proceed if at least one contour was found
	if len(cnts) > 0:
		# find the largest contour in the mask, then use
		# it to compute the minimum enclosing circle and
		# centroid
		c = max(cnts, key=cv2.contourArea)
		((x, y), radius) = cv2.minEnclosingCircle(c)
		M = cv2.moments(c)
		center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
 
		# only proceed if the radius meets a minimum size
		if radius > 10:
			# draw the circle and centroid on the frame,
			# then update the list of tracked points
			cv2.circle(frame, (int(x), int(y)), int(radius),
				(0, 255, 255), 2)
			cv2.circle(frame, center, 5, (0, 0, 255), -1)
 
	# update the points queue
	pts.appendleft(center)
	# loop over the set of tracked points
	for i in xrange(1, len(pts)):
		# if either of the tracked points are None, ignore
		# them
		if pts[i - 1] is None or pts[i] is None:
			continue
 
		# otherwise, compute the thickness of the line and
		# draw the connecting lines
		thickness = int(np.sqrt(args["buffer"] / float(i + 1)) * 2.5)
		cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness)
 
	# show the frame to our screen
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF
 
	# if the 'q' key is pressed, stop the loop
	if key == ord("q"):
		break
 
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()

Nun, ich habe es bisher versucht, aber als ich ein Video von Basketball einfügte, konnte ich es nicht erkennen, es gibt ein Farbproblem

greenLower = (7,100,100)
greenUpper = (11,255,255)

Beim Basketball haben die Bälle eine ähnliche Farbe wie die Turnhalle, und die alten Bälle sind schwarz. Daher ist es notwendig, ein anderes Erkennungsverfahren als die Farberkennung zu verwenden.

http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_video/py_table_of_contents_video/py_table_of_contents_video.html#py-table-of-content-video Ich habe auch eine verbesserte Version gefunden http://answers.opencv.org/question/17637/backgroundsubtractormog-with-python/ Derzeit gibt es auch eine Methode, um Bewegungen anhand von Hintergrundunterschieden zu erkennen. Ich habe es versucht, aber beim Basketball bewegt sich die Kamera, sodass kein Hintergrund vorhanden ist ...

Ich kann mir keine gute Idee einfallen lassen ... Bitte lassen Sie mich wissen, wenn Sie dies tun möchten.

Dies? http://qiita.com/olympic2020/items/3d8973f855e963c9d999

Ich habe es versucht, aber es hat nicht funktioniert ...

Recommended Posts

Hinweise für herausfordernde Basketball-Videoanalysen
Tipps und Vorsichtsmaßnahmen bei der Datenanalyse
Ordnerstruktur zur Analyse
Quellenanalyse für Django - INSTALLED_APPS
Installationshinweise für TensorFlow für Windows
Python für die Datenanalyse Kapitel 4
Python Tkinter Memo (für mich)
[Python] Hinweise zur Datenanalyse
Lernnotizen zur Python-Datenanalyse
Python für die Datenanalyse Kapitel 2
Hinweise zum HHKB-Programmierwettbewerb 2020
Python für die Datenanalyse Kapitel 3