Let me start with a minor change with your code.
When you initialized using
separator, your code will work only for Windows.
imgTarget = cv.imread('photosTargetImage.jpg') #bu resmimiz
myVid = cv.VideoCapture('photosvideo.mp4')
If you use os.path
's sep
, it will work on all OS.
from os.path import sep
imgTarget = cv.imread('photos' + sep + 'TargetImage.jpg') #bu resmimiz
myVid = cv.VideoCapture('photos' + sep + 'video.mp4')
I would like to suggest you to use the default resolution and initialize your cap
variable as:
cap = cv.VideoCapture(0)
You can also display the imgWarp
using imshow
cv.imshow("imgWarp", imgWarp)
cv.waitKey(0)
An example output:
Code:
import cv2 as cv
import numpy as np
from os.path import sep
cap = cv.VideoCapture(0)
imgTarget = cv.imread('photos' + sep + 'TargetImage.jpg') # bu resmimiz
myVid = cv.VideoCapture('photos' + sep + 'video.mp4')
detection = False
frameCounter = 0
# imgVideo = cap.read()
success, imgVideo = myVid.read()
hT, wT, cT = imgTarget.shape # burada resmimizin yüksekli?ini, kal?nl???n? geni?li?ini falan al?yoruz.
'''while myVid.isOpened():
success, Video = myVid.read()
if success:
Video = cv.resize(Video, (wT, hT))'''
# burada key points yani b?yle ?nemli b?lgeler, k??eler gibi yerleri beliriliyoruz.
orb = cv.ORB_create(nfeatures=1000)
kp1, des1 = orb.detectAndCompute(imgTarget, None)
while myVid.isOpened(): # burada webcam ile foto?raflar? birle?tircez.
sucess, imgWebcam = cap.read()
imgAug = imgWebcam.copy()
# burada key points yani b?yle ?nemli b?lgeler, k??eler gibi yerleri beliriliyoruz.
kp2, des2 = orb.detectAndCompute(imgWebcam,
None) # imgwebcami myvid yap?p de?i?tir o zaman oldu .d tek seferliktir belki?
# imgWebcam = cv2.drawKeypoints(imgWebcam, kp2, None)
if detection == False:
myVid.set(cv.CAP_PROP_POS_FRAMES, 0)
frameCounter = 0
else:
if frameCounter == myVid.get(cv.CAP_PROP_FRAME_COUNT):
myVid.set(cv.CAP_PROP_POS_FRAMES, 0)
frameCounter = 0
success, imgVideo = myVid.read()
imgVideo = cv.resize(imgVideo, (wT, hT))
# burada ise matches yani webcam resmi ile foto?raf?m?z aras?ndaki benzerlikleri al?yoruz.
bf = cv.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good = []
for m, n in matches:
if m.distance < 0.75 * n.distance:
good.append(m)
print(len(good))
imgFeatures = cv.drawMatches(imgTarget, kp1, imgWebcam, kp2, good, None, flags=2)
if len(good) > 20: # burada e?er anahtar b?lgelerimiz 20 den fazla ?ekilde uyu?uyorsa, tan?mlama(detection) tamamlanm?? oluyor.
detection = True
srcPts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
dstPts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
matrix, mask = cv.findHomography(srcPts, dstPts, cv.RANSAC, 5)
print(matrix)
# e?le?ine resmin etraf?n? ?iziyoruz
pts = np.float32([[0, 0], [0, hT], [wT, hT], [wT, 0]]).reshape(-1, 1, 2)
dst = cv.perspectiveTransform(pts, matrix)
img2 = cv.polylines(imgWebcam, [np.int32(dst)], True, (255, 0, 255), 3)
# burada videomuzu, webcam resmimiz ile ayn? boyuta getiriyoruz.
imgWarp = cv.warpPerspective(imgVideo, matrix, (imgWebcam.shape[1], imgWebcam.shape[0]))
cv.imshow("imgWarp", imgWarp)
cv.waitKey(0)
Is there any problem about my webcam? But it works in other codes, or in applications also. Why that this error occures and how can I solve it?
If you want to connect your default webcam, usually you initialized as:
myVid = cv.VideoCapture(0)
If you want to connect to your external-device you can use 1, 2, etc.