κ°μμμ μμ PPT μλ£λ₯Ό ν μ€νΈλ‘ λ³νν΄μ£Όλ κΈ°λ₯μ΄ EDU_Siriμ μ‘΄μ¬νλ€.
PPT μλ£λ₯Ό ν μ€νΈλ‘ λ³νν΄μ£ΌκΈ° μν΄μλ PPT λ΄μ©μ λ³νκ° μλ μ§ μ²΄ν¬ν΄μΌ νλ€. λ§μ½ PPT λ΄μ©μ λ³νκ° μμ μ, PPTμλ£λ₯Ό μ¬μ§μΌλ‘ μ°μ΄ μ μ₯-> κΈμ μΈμ -> ν μ€νΈ νμΌλ‘ μ μ₯ νλ κ³Όμ μ κ±°μΉλ€.
μ¬κΈ°μ PPT λ΄μ©μ λ³νκ° μλ μ§ κ°μ§νκ³ μμ§μ ν¬μ°© μ, ν΄λΉ νλ©΄μ μ μ₯νλ ν¨μκ° motion change detectionμ΄λ€.
λͺ¨μ μΊ‘μ³μ κ²½μ°, μ’μ λ νΌλ°μ€λ€μ΄ λ§μμ μκ³ λ¦¬μ¦μ ν¬κ² λ°κΎΈκ±°λ μλ‘ κ΅¬νν νμκ° μμλ€.
νμ§λ§, μΌλ° μμ μ½λλ€μ κ·Έλλ‘ μ΄μ©νλ©΄ νλ μ λ¨μλ‘ μ¬μ§μ μ μ₯ν΄μ λΆνμν μ λμ λ§μ μ¬μ§μ΄ μ μ₯λλ€. μ΄λ μ©λ μ°¨μ§ λ¬Έμ μ νλ‘κ·Έλ¨ μ΅μ ν λ¬Έμ λ₯Ό μΌμΌν¨λ€. λ°λΌμ λλ 2μ΄λ§λ€ λͺ¨μ λ³νλ₯Ό 체ν¬νκ³ μ μ₯νλ μ½λλ₯Ό μΆκ°νλ€.
1. Load Video File & Basic Set up
cap = cv.VideoCapture(video_name)
fgbg = cv.createBackgroundSubtractorMOG2(history=500, varThreshold=500, detectShadows=0)
# μ§μ λ μκ°λ§μ μ°κΈ° μν μΈν
start_time = int(time.time())
# νμΌ μ΄λ¦ μΈν
i = 0
while(1):
instant_time = int(time.time())
# λμμμ νμ¬ νλ μ μ(POS_FRAMES)μ μ΄ νλ μ μ(FrAME_COUNT)λ₯Ό κ°μ Έμ¨λ€.
# νμ¬ νλ μ μ == λμμ μ΄ νλ μ μ -> μ’
λ£
if(cap.get(cv.CAP_PROP_POS_FRAMES) == cap.get(cv.CAP_PROP_FRAME_COUNT)):
break
ret, frame = cap.read()
width = frame.shape[1]
height = frame.shape[0]
frame = cv.resize(frame, (int(width*0.8), int(height*0.8))) # 보μ¬μ§ μμ ν¬κΈ°
2. Subtract Background & Get Object Info
fgmask = fgbg.apply(frame) # λ°°κ²½ μ κ±°
nlabels, lables, stats, centroids = cv.connectedComponentsWithStats(fgmask)
μμ μ κ°μ²΄μ μμ§μ λ³νλ₯Ό κ°μ§νλ κ²μ΄ λͺ©μ μ΄λ€. κ°μ²΄ μΈ λλ¨Έμ§λ νμκ° μμΌλ―λ‘ fgbg.applyν¨μλ₯Ό ν΅ν΄ λλ¨Έμ§ κ°μ²΄ μΈ λλ¨Έμ§ μμλ€μ λͺ¨λ μ κ±°ν΄μ€λ€. λ°°κ²½μ μ κ±°νλ©΄ μλμ κ°μ΄ λμ¨λ€.
μ΄μ κ°μ²΄μ μ 보κ°μ κ°μ Έμ보μ. connectedComponentWithStats ν¨μλ₯Ό μ΄μ©νλ©΄ μ 4κ°μ§ μμλ₯Ό κ°μ Έμ¬ μ μλ€. μ μμλ€μ λν μ€λͺ μ μλμ κ°λ€.
retval
label - κ°μ²΄μ λ²νΈκ° μ§μ λ λ μ΄λΈλ§΅
stats - Nν 5μ΄, Nμ κ°μ²΄μ + 1, κ°κ°μ νμ λ²νΈκ° μ§μ λ κ°μ²΄λ₯Ό μλ―Έ, xμ yλ μ’μΈ‘ μλ¨ μ’ν, areaλ λ©΄μ &ν½μ
μ μ
centroid - Nν 2μ΄, x,y 무κ²μ€μ¬ μ’ν
κ°μ²΄μ μ 체 λ©΄μ μ λ³ν, μ’νκ° λ³ν, 무κ²μ€μ¬ λ³ν μ΄λ κ² 3κ°μ§λ§ μμΌλ©΄ κ°μ²΄ μμ§μμ κ°μ§ν μ μλ€. λ°λΌμ stats, centroid λ κ°λ§ κ°μ Έμ¨λ€.
3. Check Object's motion & Save
for index, centroid in enumerate(centroids):
if stats[index][0] == 0 and stats[index][1] == 0:
continue
if np.any(np.isnan(centroid)):
continue
x, y, width, height, area = stats[index]
centerX, centerY = int(centroid[0]), int(centroid[1])
# μμμ μμ§μ λ²μ = area
if(area > 400):
if(instant_time > start_time+2):
cv.imwrite("%s/%s.png" % (dir_name, i), frame)
start_time = instant_time
cv.imshow('mask', fgmask)
cv.imshow('frame', frame)
λ°μμ¨ κ°μ²΄μ μμ§μ μ 보κ°κ³Ό areaκ°μ λΉκ΅νμ¬ μ μ κ°μ μ΄κ³Ό μ, ν΄λΉ νλ©΄μ μ€ν¬λ¦°μ·μΌλ‘ μ°μ΄ νμΌμ μ μ₯νλ€. μ μ½λλ₯Ό λλ €λ³΄λ©΄ μλμ κ°μ΄ λ³νλ κ°μ κ°μ§νλ€.
Full Code
def motion_change_detection(video_name, dir_name) -> str:
base_path = os.getcwd()
video_name = base_path + "\\video\\" + str(video_name)
cap = cv.VideoCapture(video_name)
fgbg = cv.createBackgroundSubtractorMOG2(history=500, varThreshold=500, detectShadows=0)
# μ§μ λ μκ°λ§μ μ°κΈ° μν μΈν
start_time = int(time.time())
# νμΌ μ΄λ¦ μΈν
i = 0
try:
while(1):
instant_time = int(time.time())
# λμμμ νμ¬ νλ μ μ(POS_FRAMES)μ μ΄ νλ μ μ(FrAME_COUNT)λ₯Ό κ°μ Έμ¨λ€.
# νμ¬ νλ μ μ == λμμ μ΄ νλ μ μ -> μ’
λ£
if(cap.get(cv.CAP_PROP_POS_FRAMES) == cap.get(cv.CAP_PROP_FRAME_COUNT)):
break
ret, frame = cap.read()
width = frame.shape[1]
height = frame.shape[0]
frame = cv.resize(frame, (int(width*0.8), int(height*0.8))) # 보μ¬μ§ μμ ν¬κΈ°
fgmask = fgbg.apply(frame) # λ°°κ²½ μ κ±°
nlabels, lables, stats, centroids = cv.connectedComponentsWithStats(fgmask)
for index, centroid in enumerate(centroids):
if stats[index][0] == 0 and stats[index][1] == 0:
continue
if np.any(np.isnan(centroid)):
continue
x, y, width, height, area = stats[index]
centerX, centerY = int(centroid[0]), int(centroid[1])
# μμμ μμ§μ λ²μ = area
if(area > 400):
if(instant_time > start_time+2):
cv.imwrite("%s/%s.png" % (dir_name, i), frame)
start_time = instant_time
i += 1
cv.imshow('mask', fgmask)
cv.imshow('frame', frame)
k = cv.waitKey(30) & 0xff
if k == 27:
break
cap.release()
print("\n+==================================+")
print("| Detecting motion change is done! |")
print("+==================================+")
#cap.destoryAllWindows()
except OSError:
error("Failed to save file in directory that you want. Please check permission of directory or check filename")