Motion Change Detection Function

2023. 3. 25. 00:59Β·πŸ“„ Project/Edu_Siri

κ°•μ˜μžμ˜ μˆ˜μ—… 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")
μ €μž‘μžν‘œμ‹œ λΉ„μ˜λ¦¬ λ³€κ²½κΈˆμ§€ (μƒˆμ°½μ—΄λ¦Ό)
'πŸ“„ Project/Edu_Siri' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • Text Summarization Function (Extractive / eng ver)
  • Detecting Text & Getting Similarity between images
  • Converting Audio into Text ( with Predicting Punctuation )
  • Text Summarization Function (Extractive)
Cronus
Cronus
Offensive Security Researcher
  • Cronus
    Cronus
    Striving to be the best.
    • λΆ„λ₯˜ 전체보기 (251)
      • AboutMe (1)
      • Portfolio (1)
        • Things (1)
      • Bug Report (1)
      • 🚩 CTF (23)
        • Former Doc (9)
        • 2023 (9)
      • πŸ’» Security (5)
      • πŸ–ŒοΈ Theory (22)
        • WEB (9)
        • PWN (13)
      • πŸ“„ Project (6)
        • Edu_Siri (6)
      • Dreamhack (156)
        • WEB (95)
        • PWN (41)
        • Crypto (14)
        • ETC (6)
      • Wargame (22)
        • HackCTF (22)
      • Bug Bounty (1)
        • Hacking Zone (1)
      • Tips (7)
      • Development (2)
        • Machine Learning & Deep Lea.. (1)
      • Offensive Tools (1)
  • λΈ”λ‘œκ·Έ 메뉴

    • ν™ˆ
  • 링크

  • 곡지사항

  • 인기 κΈ€

  • νƒœκ·Έ

    Ubuntu 기초 μ…‹νŒ…
    sqli
    Text Summarization
    cache poisoning
    Crypto
    TFCCTF2022
    justCTF
    Remote Code Execution
    Ubuntu 기초
    ubuntu λͺ…λ Ήμ–΄
    GPNCTF
    TsukuCTF2022
    RCE
    Machine Learning
    pwntools
    python
    cache
    Deep learning
    bug report
    bug hunter
  • 졜근 λŒ“κΈ€

  • 졜근 κΈ€

Cronus
Motion Change Detection Function
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”