Python:yolo3による物体認識(動画&画像)

このサイトを参考にして、YOLOv3による物体検出を学習する。このサイトの場合、通りを行き交う歩行者の検出を行っている。最近は、通りを行き交う人々の顔認識なんかも実験的に行われおり、AIの進歩の目まぐるしさに驚愕させられる今日この頃だ。

スポンサーリンク

必要なデータのダウンロード

以下のセルでは、モジュールのインポートと必要なデータのダウンロード、ダウロードしたデータのロード等が行われている。

import os
from os.path import exists, join, basename

project_name = "deep_sort_pytorch"
if not exists(project_name):
  # クローンしてインストール
  !git clone -q --recursive https://github.com/ZQPei/deep_sort_pytorch.git
  
import sys
sys.path.append(project_name)
sys.path.append(join(project_name, 'YOLOv3'))

import IPython
from IPython.display import clear_output
if not exists('yolov3.weights'):
  !wget -q https://pjreddie.com/media/files/yolov3.weights
    
if not exists('ckpt.t7'):
  file_id = '1_qwTWdzT9dWNudpusgKavj_4elGgbkUN'
  !curl -Lb ./cookie "https://drive.google.com/uc?export=download&id={file_id}" -o ckpt.t7
import cv2
import time

from YOLOv3 import YOLOv3
from deep_sort import DeepSort
from util import draw_bboxes

yolo3 = YOLOv3("deep_sort_pytorch/YOLOv3/cfg/yolo_v3.cfg","yolov3.weights","deep_sort_pytorch/YOLOv3/cfg/coco.names", is_xywh=True)
deepsort = DeepSort("ckpt.t7")
Loading weights from yolov3.weights... Done!
Loading weights from ckpt.t7... Done!
VIDEO_URL = 'http://www.robots.ox.ac.uk/ActiveVision/Research/Projects/2009bbenfold_headpose/Datasets/TownCentreXVID.avi'
DURATION_S = 20  # 最初の20秒間のみ抽出

video_file_name = 'video.mp4'
if not exists(video_file_name):
  !wget -q $VIDEO_URL
  dowloaded_file_name = basename(VIDEO_URL)
  # aviをmp4に変換する。
  !ffmpeg -y -loglevel info -t $DURATION_S -i $dowloaded_file_name $video_file_name
def show_local_mp4_video(file_name, width=640, height=480):
  import io
  import base64
  from IPython.display import HTML
  video_encoded = base64.b64encode(io.open(file_name, 'rb').read())
  return HTML(data='''<video width="{0}" height="{1}" alt="test" controls>
                        <source src="data:video/mp4;base64,{2}" type="video/mp4" />
                      </video>'''.format(width, height, video_encoded.decode('ascii')))
 
clear_output()
video = show_local_mp4_video('video.mp4')
from IPython.display import Video

Video("https://eigo.rumisunheart.com/wp-content/uploads/2019/09/video.mp4", width=800, height=600)

この通りを行き交う人々を認識することになる。

スポンサーリンク

yolo3による物体認識(動画編)

video_capture = cv2.VideoCapture()
if video_capture.open('video.mp4'):
  width, height = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
  fps = video_capture.get(cv2.CAP_PROP_FPS)
  !rm -f output.mp4 output.avi
  # mp4でセーブできないのでaviでセーブする。
  video_writer = cv2.VideoWriter("output.avi", cv2.VideoWriter_fourcc(*'MJPG'), fps, (width, height))
  while video_capture.isOpened():
    ret, frame = video_capture.read()
    if not ret:
      break
      
    start = time.time()
    xmin, ymin, xmax, ymax = 0, 0, width, height
    im = frame[ymin:ymax, xmin:xmax, (2,1,0)]
    bbox_xywh, cls_conf, cls_ids = yolo3(im)
    if bbox_xywh is not None:
        mask = cls_ids==0
        bbox_xywh = bbox_xywh[mask]
        bbox_xywh[:,3] *= 1.2
        cls_conf = cls_conf[mask]
        outputs = deepsort.update(bbox_xywh, cls_conf, im)
        if len(outputs) > 0:
            bbox_xyxy = outputs[:,:4]
            identities = outputs[:,-1]
            frame = draw_bboxes(frame, bbox_xyxy, identities, offset=(xmin,ymin))

    end = time.time()
    print("time: {}s, fps: {}".format(end-start, 1/(end-start)))
            
    video_writer.write(frame)
  video_capture.release()
  video_writer.release()
  
  # aviをmp4に変換する。
  !ffmpeg -y -loglevel info -i output.avi output.mp4
else:
  print("can't open the given input video file!")
time: 1.4075300693511963s, fps: 0.7104643955925943
time: 0.34830641746520996s, fps: 2.8710352432707715
time: 0.38646364212036133s, fps: 2.587565532719782
time: 0.28902769088745117s, fps: 3.4598761002086995
time: 0.4138491153717041s, fps: 2.4163395857493537
time: 0.2641174793243408s, fps: 3.786193941264988
time: 0.2683408260345459s, fps: 3.726604016159886
time: 0.27571940422058105s, fps: 3.6268756739368984
time: 0.23331546783447266s, fps: 4.286042452656663
time: 0.2470567226409912s, fps: 4.047653467228832
time: 0.24388957023620605s, fps: 4.100216335743689
time: 0.24810194969177246s, fps: 4.030601134905801
time: 0.23734760284423828s, fps: 4.213229828389124
time: 0.24974918365478516s, fps: 4.004017091732504
time: 0.4456150531768799s, fps: 2.2440893611443276
time: 0.23152828216552734s, fps: 4.319126763464113
time: 0.23567771911621094s, fps: 4.243082476145772
time: 0.23165440559387207s, fps: 4.3167752300518
time: 0.24050354957580566s, fps: 4.157942790298836
time: 0.223616361618042s, fps: 4.471944685819078
time: 0.26142239570617676s, fps: 3.82522697528922
time: 0.2513861656188965s, fps: 3.977943645140792
time: 0.2449483871459961s, fps: 4.082492690200781
time: 0.2506732940673828s, fps: 3.989256229788853
time: 0.23098969459533691s, fps: 4.329197463773726
time: 0.38730287551879883s, fps: 2.581958625172826
time: 0.2694244384765625s, fps: 3.711615789771762
time: 0.24756979942321777s, fps: 4.039264895515431
time: 0.24406743049621582s, fps: 4.0972283682705655
time: 0.25161051750183105s, fps: 3.9743966584891375
time: 0.23313045501708984s, fps: 4.2894438649240145
time: 0.24453139305114746s, fps: 4.089454476672591
time: 0.2502453327178955s, fps: 3.996078524778369
time: 0.25614213943481445s, fps: 3.9040823279079766
time: 0.23886466026306152s, fps: 4.186471112548422
time: 0.22530746459960938s, fps: 4.438379357635068
time: 0.22469830513000488s, fps: 4.450411850776644
time: 0.23769712448120117s, fps: 4.207034486355712
time: 0.2731325626373291s, fps: 3.6612258543768728
time: 0.20638346672058105s, fps: 4.845349367805137
time: 0.221205472946167s, fps: 4.520683808955134
time: 0.23832392692565918s, fps: 4.195969800010604
time: 0.23099088668823242s, fps: 4.329175121742774
time: 0.23087477684020996s, fps: 4.331352318716509
time: 0.24223613739013672s, fps: 4.128203210198305
time: 0.227217435836792s, fps: 4.401070702682738
time: 0.24837446212768555s, fps: 4.026178824640655
time: 0.2425377368927002s, fps: 4.123069724372025
time: 0.23981022834777832s, fps: 4.169963920595484
time: 0.3161139488220215s, fps: 3.163416241916677
time: 0.23908019065856934s, fps: 4.182697015781208
time: 0.2358248233795166s, fps: 4.240435699980083
time: 0.27050328254699707s, fps: 3.6968128097530966
time: 0.2887270450592041s, fps: 3.4634788015613425
time: 0.21412301063537598s, fps: 4.670212683039805
time: 0.2174673080444336s, fps: 4.598392323850704
time: 0.25803136825561523s, fps: 3.8754977999781937
time: 0.2671670913696289s, fps: 3.742975958878438
time: 0.2361125946044922s, fps: 4.235267507330905
time: 0.2738168239593506s, fps: 3.6520765435087172
time: 0.2780778408050537s, fps: 3.596115379438124
time: 0.28763747215270996s, fps: 3.476598485294325
time: 0.24864912033081055s, fps: 4.0217315012800725
time: 0.1974165439605713s, fps: 5.065431599287461
time: 0.252673864364624s, fps: 3.957670899262213
time: 0.2543337345123291s, fps: 3.931841766557019
time: 0.2797842025756836s, fps: 3.574183212611845
time: 0.23332738876342773s, fps: 4.285823474473916
time: 0.22899317741394043s, fps: 4.366942331178479
time: 0.2594869136810303s, fps: 3.8537588883161655
time: 0.2921426296234131s, fps: 3.4229855508901648
time: 0.24145126342773438s, fps: 4.141622561023778
time: 0.24199795722961426s, fps: 4.13226628624461
time: 0.25365114212036133s, fps: 3.9424226188797715
time: 0.28438329696655273s, fps: 3.5163809220399234
time: 0.26835155487060547s, fps: 3.726455024574696
time: 0.2306532859802246s, fps: 4.335511613243336
time: 0.2289872169494629s, fps: 4.367056001299401
time: 0.21898245811462402s, fps: 4.566575828080991
time: 0.20435237884521484s, fps: 4.893507996583893
time: 0.22274374961853027s, fps: 4.489463797357253
time: 0.20661544799804688s, fps: 4.839909163081809
time: 0.21069598197937012s, fps: 4.7461749892217355
time: 0.2557713985443115s, fps: 3.9097412990325164
time: 0.21446943283081055s, fps: 4.662669112333945
time: 0.2126927375793457s, fps: 4.701617983674439
time: 0.22635245323181152s, fps: 4.417888941437195
time: 0.254314661026001s, fps: 3.932136652938623
time: 0.2267591953277588s, fps: 4.40996449363209
time: 0.24773049354553223s, fps: 4.036644765397856
time: 0.2124333381652832s, fps: 4.707359064432498
time: 0.217911958694458s, fps: 4.589009276916899
time: 0.20125746726989746s, fps: 4.968759736298104
time: 0.20175695419311523s, fps: 4.956458645994588
time: 0.21979618072509766s, fps: 4.549669592533616
time: 0.22647404670715332s, fps: 4.415516985454274
time: 0.2053074836730957s, fps: 4.870743053829771
time: 0.21654510498046875s, fps: 4.617975548743966
time: 0.21761679649353027s, fps: 4.595233530283725
time: 0.23873281478881836s, fps: 4.1887831837638
time: 0.21864032745361328s, fps: 4.573721653486637
time: 0.24027085304260254s, fps: 4.161969657729102
time: 0.22663140296936035s, fps: 4.412451173570134
time: 0.2294316291809082s, fps: 4.358596953567784
time: 0.19270706176757812s, fps: 5.189223429736525
time: 0.21264290809631348s, fps: 4.7027197330609525
time: 0.1943223476409912s, fps: 5.146088507779306
time: 0.20573163032531738s, fps: 4.860701285547242
time: 0.20044350624084473s, fps: 4.988936876799794
time: 0.22880244255065918s, fps: 4.3705827125450805
time: 0.23548293113708496s, fps: 4.246592290877576
time: 0.2409219741821289s, fps: 4.15072142503711
time: 0.21210479736328125s, fps: 4.714650552138412
time: 0.22443604469299316s, fps: 4.4556122942190655
time: 0.21967434883117676s, fps: 4.552192849646346
time: 0.2633392810821533s, fps: 3.797382585274213
time: 0.2051372528076172s, fps: 4.874784985727701
time: 0.20674490928649902s, fps: 4.836878467533336
time: 0.2044544219970703s, fps: 4.891065647943429
time: 0.20297670364379883s, fps: 4.9266737613144365
time: 0.21678614616394043s, fps: 4.61284089271908
time: 0.2318096160888672s, fps: 4.313884889126589
time: 0.2181863784790039s, fps: 4.583237537425967
time: 0.2122631072998047s, fps: 4.711134274443556
time: 0.22455644607543945s, fps: 4.453223309670885
time: 0.26760315895080566s, fps: 3.736876664388828
time: 0.21284890174865723s, fps: 4.698168474370851
time: 0.21753811836242676s, fps: 4.5968955120498105
time: 0.24532508850097656s, fps: 4.076223944767961
time: 0.21963930130004883s, fps: 4.552919236589184
time: 0.2431926727294922s, fps: 4.111965992957028
time: 0.23706984519958496s, fps: 4.218166166001068
time: 0.2013852596282959s, fps: 4.965606727353017
time: 0.20471405982971191s, fps: 4.8848623334998775
time: 0.20112967491149902s, fps: 4.971916751916491
time: 0.20225811004638672s, fps: 4.944177515406704
time: 0.19645071029663086s, fps: 5.09033537465988
time: 0.21505951881408691s, fps: 4.649875557772789
time: 0.23447442054748535s, fps: 4.264857538255359
time: 0.23585104942321777s, fps: 4.239964174191872
time: 0.23041844367980957s, fps: 4.339930363341939
time: 0.21695899963378906s, fps: 4.609165794864131
time: 0.22712278366088867s, fps: 4.402904824788846
time: 0.22505521774291992s, fps: 4.443353991207162
time: 0.29282665252685547s, fps: 3.4149896922661056
time: 0.33829522132873535s, fps: 2.9559980069250194
time: 0.36717653274536133s, fps: 2.7234856011168467
time: 0.36149120330810547s, fps: 2.766319044139179
time: 0.36031031608581543s, fps: 2.77538542571684
time: 0.3426828384399414s, fps: 2.9181502188801907
time: 0.3396456241607666s, fps: 2.9442452040149463
time: 0.33144044876098633s, fps: 3.0171332549731615
time: 0.29566502571105957s, fps: 3.38220591899583
time: 0.297619104385376s, fps: 3.3599993591309816
time: 0.2989990711212158s, fps: 3.3444919954102286
time: 0.31899261474609375s, fps: 3.1348688144268255
time: 0.29094648361206055s, fps: 3.4370582094175455
time: 0.29152965545654297s, fps: 3.43018276625743
time: 0.30132341384887695s, fps: 3.318693317677368
time: 0.32724881172180176s, fps: 3.0557788574954774
time: 0.3180580139160156s, fps: 3.1440805018170477
time: 0.28006982803344727s, fps: 3.5705381298001697
time: 0.27967071533203125s, fps: 3.5756335761246145
time: 0.31476497650146484s, fps: 3.176973534713911
time: 0.27768921852111816s, fps: 3.601148093993971
time: 0.2638700008392334s, fps: 3.7897449381116437
time: 0.27553367614746094s, fps: 3.629320430018206
time: 0.2818565368652344s, fps: 3.547904232138265
time: 0.26028943061828613s, fps: 3.841877088995203
time: 0.2813589572906494s, fps: 3.5541786535943833
time: 0.2461261749267578s, fps: 4.062956734681225
time: 0.23911190032958984s, fps: 4.182142330104058
time: 0.25063276290893555s, fps: 3.989901353652388
time: 0.29924511909484863s, fps: 3.341742057563988
time: 0.27835726737976074s, fps: 3.5925054496087845
time: 0.3050532341003418s, fps: 3.278116368604268
time: 0.3021559715270996s, fps: 3.3095490218048282
time: 0.3192934989929199s, fps: 3.1319146902586144
time: 0.2540416717529297s, fps: 3.9363620665059953
time: 0.2937600612640381s, fps: 3.404138723613547
time: 0.2943887710571289s, fps: 3.3968686930859215
time: 0.2650890350341797s, fps: 3.772317477677126
time: 0.2834928035736084s, fps: 3.5274264016382757
time: 0.2502257823944092s, fps: 3.9963907413177227
time: 0.28388118743896484s, fps: 3.522600454864599
time: 0.2497258186340332s, fps: 4.00439171836483
time: 0.2945883274078369s, fps: 3.394567628660894
time: 0.2631089687347412s, fps: 3.800706622844814
time: 0.24474334716796875s, fps: 4.085912902521899
time: 0.24343252182006836s, fps: 4.107914556869044
time: 0.24848294258117676s, fps: 4.02442111161538
time: 0.2426621913909912s, fps: 4.120955119822283
time: 0.27900028228759766s, fps: 3.5842257642205
time: 0.2795436382293701s, fps: 3.5772590152077925
time: 0.24142241477966309s, fps: 4.142117462095064
time: 0.2772555351257324s, fps: 3.606781013574754
time: 0.24613571166992188s, fps: 4.0627993118732855
time: 0.2272627353668213s, fps: 4.40019345180333
time: 0.23894095420837402s, fps: 4.185134370594028
time: 0.27187466621398926s, fps: 3.67816543529257
time: 0.30347394943237305s, fps: 3.2951757535380897
time: 0.286684513092041s, fps: 3.4881549380344334
time: 0.2646000385284424s, fps: 3.7792889432724253
time: 0.24629521369934082s, fps: 4.060168222435402
time: 0.24159955978393555s, fps: 4.139080389444038
time: 0.24630403518676758s, fps: 4.060022805723501
time: 0.2511613368988037s, fps: 3.9815045275177585
time: 0.22708368301391602s, fps: 4.403662943667857
time: 0.24561262130737305s, fps: 4.071452007136658
time: 0.2531125545501709s, fps: 3.950811534327841
time: 0.24234247207641602s, fps: 4.126391843047131
time: 0.24679994583129883s, fps: 4.051864746694695
time: 0.24294710159301758s, fps: 4.1161223716724535
time: 0.23258757591247559s, fps: 4.299455790262448
time: 0.259655237197876s, fps: 3.8512606592946477
time: 0.3153994083404541s, fps: 3.170582992725725
time: 0.3059821128845215s, fps: 3.2681648955649996
time: 0.27046895027160645s, fps: 3.6972820687764507
time: 0.273052453994751s, fps: 3.6622999917049768
time: 0.277799129486084s, fps: 3.599723303128975
time: 0.29483985900878906s, fps: 3.391671680219432
time: 0.2979574203491211s, fps: 3.356184245481402
time: 0.3081812858581543s, fps: 3.244843362942768
time: 0.29413795471191406s, fps: 3.3997652597381545
time: 0.26169919967651367s, fps: 3.8211809636257956
time: 0.26134634017944336s, fps: 3.826340171105471
time: 0.2683238983154297s, fps: 3.7268391160016776
time: 0.28992438316345215s, fps: 3.4491752266184004
time: 0.2788410186767578s, fps: 3.5862729405648697
time: 0.2711024284362793s, fps: 3.6886427235934662
time: 0.23794269561767578s, fps: 4.202692574378459
time: 0.21624994277954102s, fps: 4.624278680246698
time: 0.2112751007080078s, fps: 4.733165416316839
time: 0.21610617637634277s, fps: 4.627355019499898
time: 0.24135804176330566s, fps: 4.143222213331832
time: 0.21972298622131348s, fps: 4.551185186390837
time: 0.2238450050354004s, fps: 4.467376879112639
time: 0.2637166976928711s, fps: 3.7919479833795613
time: 0.21914196014404297s, fps: 4.5632520551641305
time: 0.24143481254577637s, fps: 4.141904762845245
time: 0.22142696380615234s, fps: 4.516161820632863
time: 0.2664008140563965s, fps: 3.753742283191005
time: 0.265045166015625s, fps: 3.7729418537708694
time: 0.26421475410461426s, fps: 3.784799994946747
time: 0.22908544540405273s, fps: 4.365183472202853
time: 0.23214507102966309s, fps: 4.307651226728918
time: 0.24053263664245605s, fps: 4.1574399797
time: 0.2666206359863281s, fps: 3.750647418196386
time: 0.2576014995574951s, fps: 3.881964979698443
time: 0.2645719051361084s, fps: 3.779690815944922
time: 0.2686276435852051s, fps: 3.7226250681189237
time: 0.22893548011779785s, fps: 4.36804290661043
time: 0.22345328330993652s, fps: 4.475208353116788
time: 0.2764103412628174s, fps: 3.6178096500708588
time: 0.26587438583374023s, fps: 3.7611746496908958
time: 0.2571539878845215s, fps: 3.8887205608845687
time: 0.2465519905090332s, fps: 4.055939673962445
time: 0.24814605712890625s, fps: 4.0298847040737895
time: 0.24984478950500488s, fps: 4.002484910656774
time: 0.26587486267089844s, fps: 3.7611679041572508
time: 0.24909520149230957s, fps: 4.014529360698558
time: 0.25002169609069824s, fps: 3.9996528926723163
time: 0.25992536544799805s, fps: 3.8472582245924163
time: 0.24831819534301758s, fps: 4.027091122415081
time: 0.22288012504577637s, fps: 4.486716793588546
time: 0.2272663116455078s, fps: 4.400124210049264
time: 0.21678853034973145s, fps: 4.6127901618538685
time: 0.24326229095458984s, fps: 4.1107892064811296
time: 0.2556276321411133s, fps: 3.9119401593016097
time: 0.21201586723327637s, fps: 4.716628113969046
time: 0.23882079124450684s, fps: 4.187240125907594
time: 0.24329447746276855s, fps: 4.110245371899287
time: 0.23281097412109375s, fps: 4.295330165492381
time: 0.21960186958312988s, fps: 4.553695293661659
time: 0.25360631942749023s, fps: 3.943119407503229
time: 0.25221920013427734s, fps: 3.9648052149385
time: 0.21938133239746094s, fps: 4.55827298098575
time: 0.22450828552246094s, fps: 4.4541785960053355
time: 0.2344801425933838s, fps: 4.264753462446148
time: 0.25379514694213867s, fps: 3.9401856656777774
time: 0.2510499954223633s, fps: 3.9832703375182814
time: 0.23598122596740723s, fps: 4.237625242857734
time: 0.267101526260376s, fps: 3.7438947429494647
time: 0.26273274421691895s, fps: 3.8061491078339826
time: 0.2594332695007324s, fps: 3.8545557473197434
time: 0.23024487495422363s, fps: 4.343201993958893
time: 0.26023364067077637s, fps: 3.8427007262489474
time: 0.2642703056335449s, fps: 3.7840044026235304
time: 0.26675939559936523s, fps: 3.7486964526709983
time: 0.25725245475769043s, fps: 3.8872321002414285
time: 0.2691969871520996s, fps: 3.7147518275714866
time: 0.27152085304260254s, fps: 3.6829583761034246
time: 0.27789878845214844s, fps: 3.598432384573676
time: 0.24467039108276367s, fps: 4.0871312445065495
time: 0.29370760917663574s, fps: 3.404746655367039
time: 0.25611281394958496s, fps: 3.9045293539933814
time: 0.24387502670288086s, fps: 4.1004608529200715
time: 0.30478835105895996s, fps: 3.2809652879632347
time: 0.26674962043762207s, fps: 3.7488338253656277
time: 0.2632431983947754s, fps: 3.7987686143379085
time: 0.26867127418518066s, fps: 3.722020536184132
time: 0.2815978527069092s, fps: 3.5511634424315495
time: 0.2840149402618408s, fps: 3.5209415359560796
time: 0.2958838939666748s, fps: 3.37970406767943
time: 0.28122878074645996s, fps: 3.5558238290751034
time: 0.29189372062683105s, fps: 3.425904462256114
time: 0.29491424560546875s, fps: 3.3908161945414563
time: 0.264507532119751s, fps: 3.780610676701895
time: 0.2803499698638916s, fps: 3.5669702425346954
time: 0.2733919620513916s, fps: 3.657752014713667
time: 0.30179738998413086s, fps: 3.313481273156743
time: 0.2998940944671631s, fps: 3.3345104770294003
time: 0.27073049545288086s, fps: 3.6937102276830296
time: 0.3044407367706299s, fps: 3.2847115356753807
time: 0.2988615036010742s, fps: 3.3460314826456146
time: 0.304884672164917s, fps: 3.2799287445289607
time: 0.28867316246032715s, fps: 3.4641252809132603
time: 0.3433959484100342s, fps: 2.9120902696439024
time: 0.324904203414917s, fps: 3.077830294251244
time: 0.292236328125s, fps: 3.4218880534670006
time: 0.30054140090942383s, fps: 3.3273286042257344
time: 0.3040330410003662s, fps: 3.2891161983897517
time: 0.2934234142303467s, fps: 3.408044319240892
time: 0.2882676124572754s, fps: 3.468998794126453
time: 0.3080112934112549s, fps: 3.246634202677776
time: 0.3482828140258789s, fps: 2.871229815909595
time: 0.29004764556884766s, fps: 3.4477094204256633
time: 0.2943298816680908s, fps: 3.39754833703116
time: 0.3174436092376709s, fps: 3.150165796065207
time: 0.2956357002258301s, fps: 3.382541415790178
time: 0.28063011169433594s, fps: 3.5634094786278894
time: 0.2884526252746582s, fps: 3.4667737866757915
time: 0.32074594497680664s, fps: 3.117732322609131
time: 0.27494120597839355s, fps: 3.637141244221449
time: 0.3194239139556885s, fps: 3.1306359865677535
time: 0.3424713611602783s, fps: 2.9199521869859213
time: 0.28815627098083496s, fps: 3.470339189899182
time: 0.30928874015808105s, fps: 3.233224719040494
time: 0.3104400634765625s, fps: 3.221233718358319
time: 0.2829902172088623s, fps: 3.5336910578147127
time: 0.3165721893310547s, fps: 3.1588371742732337
time: 0.28985095024108887s, fps: 3.4500490654532325
time: 0.31053924560546875s, fps: 3.220204898901801
time: 0.31591796875s, fps: 3.1653786707882534
time: 0.3123047351837158s, fps: 3.202000761889639
time: 0.3238494396209717s, fps: 3.0878546560722304
time: 0.34756040573120117s, fps: 2.8771977000550155
time: 0.27988767623901367s, fps: 3.5728618474292424
time: 0.28696393966674805s, fps: 3.4847584026108036
time: 0.3251922130584717s, fps: 3.0751043839422856
time: 0.2962830066680908s, fps: 3.3751513839612266
time: 0.30417537689208984s, fps: 3.2875770886437103
time: 0.3335409164428711s, fps: 2.998132914740252
time: 0.3583686351776123s, fps: 2.79042277096707
time: 0.3301072120666504s, fps: 3.0293188498956356
time: 0.32623791694641113s, fps: 3.0652476246783515
time: 0.32375025749206543s, fps: 3.0888006321493298
time: 0.29712343215942383s, fps: 3.365604633509492
time: 0.296189546585083s, fps: 3.376216384168512
time: 0.3165161609649658s, fps: 3.159396338409042
time: 0.3254358768463135s, fps: 3.072801959300413
time: 0.34043335914611816s, fps: 2.937432461108454
time: 0.3236711025238037s, fps: 3.0895560097968806
time: 0.3327212333679199s, fps: 3.0055190342908165
time: 0.3234250545501709s, fps: 3.0919064121078357
time: 0.31740808486938477s, fps: 3.150518363171202
time: 0.34438347816467285s, fps: 2.903739765128433
time: 0.36223864555358887s, fps: 2.760611028875057
time: 0.3311471939086914s, fps: 3.0198051452482915
time: 0.3119316101074219s, fps: 3.205830918051632
time: 0.32091689109802246s, fps: 3.1160715678706827
time: 0.2973823547363281s, fps: 3.3626742948035457
time: 0.2914445400238037s, fps: 3.4311845400099967
time: 0.2977614402770996s, fps: 3.358393212597946
time: 0.33039355278015137s, fps: 3.0266934435776185
time: 0.3130347728729248s, fps: 3.194533280831219
time: 0.3394894599914551s, fps: 2.9455995482898643
time: 0.30089402198791504s, fps: 3.323429270522907
time: 0.3181321620941162s, fps: 3.1433476999542096
time: 0.2880215644836426s, fps: 3.471962253217996
time: 0.2598094940185547s, fps: 3.8489740483794
time: 0.2638068199157715s, fps: 3.7906525703895033
time: 0.2821958065032959s, fps: 3.54363876767361
time: 0.34305548667907715s, fps: 2.9149803423359435
time: 0.2646937370300293s, fps: 3.7779511189815222
time: 0.3095228672027588s, fps: 3.230779066623634
time: 0.3139369487762451s, fps: 3.1853529949185377
time: 0.3445558547973633s, fps: 2.9022870634083695
time: 0.29160284996032715s, fps: 3.4293217646399925
time: 0.29236698150634766s, fps: 3.4203588751635032
time: 0.327716588973999s, fps: 3.051417089170728
time: 0.3178138732910156s, fps: 3.1464957449617708
time: 0.2889571189880371s, fps: 3.460721104578151
time: 0.3522605895996094s, fps: 2.8388074894686115
time: 0.3751063346862793s, fps: 2.6659107232522516
time: 0.37576746940612793s, fps: 2.66122025299429
time: 0.3315300941467285s, fps: 3.016317425341846
time: 0.38402676582336426s, fps: 2.6039851619612286
time: 0.3161313533782959s, fps: 3.163242080589705
time: 0.3265197277069092s, fps: 3.0626020884643776
time: 0.3457517623901367s, fps: 2.8922484533039854
time: 0.3030967712402344s, fps: 3.2992763199295196
time: 0.30747008323669434s, fps: 3.2523489422877847
time: 0.3354756832122803s, fps: 2.980841980630906
time: 0.3039703369140625s, fps: 3.2897946890216354
time: 0.305464506149292s, fps: 3.2737027702696904
time: 0.31020379066467285s, fps: 3.223687234309106
time: 0.30459046363830566s, fps: 3.2830968772137186
time: 0.2865767478942871s, fps: 3.4894666344977914
time: 0.35152673721313477s, fps: 2.8447338257336834
time: 0.3244311809539795s, fps: 3.0823177878881185
time: 0.31462669372558594s, fps: 3.178369858446243
time: 0.33233022689819336s, fps: 3.009055207928293
time: 0.32756710052490234s, fps: 3.0528096331944603
time: 0.3519172668457031s, fps: 2.8415769676866875
time: 0.34876251220703125s, fps: 2.8672806422680637
time: 0.2782323360443115s, fps: 3.594118549329001
time: 0.31479454040527344s, fps: 3.1766751695012814
time: 0.34151673316955566s, fps: 2.9281142119133636
time: 0.3011448383331299s, fps: 3.3206612656391887
time: 0.32889294624328613s, fps: 3.040503031221253
time: 0.33630943298339844s, fps: 2.9734521304651125
time: 0.37299561500549316s, fps: 2.6809966652966493
time: 0.3751094341278076s, fps: 2.665888695455415
time: 0.30919528007507324s, fps: 3.234202021962295
time: 0.3188817501068115s, fps: 3.1359587046453536
time: 0.3486349582672119s, fps: 2.8683296849237596
time: 0.3187847137451172s, fps: 3.1369132737009005
time: 0.30036020278930664s, fps: 3.329335879765233
time: 0.30845069885253906s, fps: 3.2420091888917058
time: 0.29039835929870605s, fps: 3.443545626135553
time: 0.31873559951782227s, fps: 3.1373966432139455
time: 0.3717832565307617s, fps: 2.6897392027046787
time: 0.32840561866760254s, fps: 3.04501489364637
time: 0.32068634033203125s, fps: 3.1183118026312657
time: 0.2976377010345459s, fps: 3.3597894235983667
time: 0.28356480598449707s, fps: 3.526530722062425
time: 0.38638997077941895s, fps: 2.588058892892116
time: 0.3070845603942871s, fps: 3.256432035254494
time: 0.28663110733032227s, fps: 3.4888048590189134
time: 0.284576416015625s, fps: 3.513994638069705
time: 0.2540559768676758s, fps: 3.936140422001749
time: 0.23759841918945312s, fps: 4.20878221080517
time: 0.251131534576416s, fps: 3.981977021271748
time: 0.29113245010375977s, fps: 3.434862721910936
time: 0.32744932174682617s, fps: 3.053907684600946
time: 0.30651044845581055s, fps: 3.2625315222954607
time: 0.3233461380004883s, fps: 3.092661029396584
time: 0.35270071029663086s, fps: 2.835265058465499
time: 0.3078000545501709s, fps: 3.2488623222027457
time: 0.28841161727905273s, fps: 3.4672667121881213
time: 0.2873811721801758s, fps: 3.47969907845265
time: 0.3027799129486084s, fps: 3.3027290029300342
time: 0.32586026191711426s, fps: 3.0688000866284204
time: 0.2764308452606201s, fps: 3.6175413024447254
time: 0.2893991470336914s, fps: 3.455435201692497
time: 0.2939746379852295s, fps: 3.4016539891112787
time: 0.3099842071533203s, fps: 3.22597079762
time: 0.29731225967407227s, fps: 3.3634670870829453
time: 0.28502655029296875s, fps: 3.508445086859927
time: 0.31109619140625s, fps: 3.2144398665881893
time: 0.2830982208251953s, fps: 3.5323429341418224
time: 0.28795647621154785s, fps: 3.47274703856755
time: 0.28049349784851074s, fps: 3.5651450307061348
time: 0.27026844024658203s, fps: 3.7000250531939294
time: 0.29052019119262695s, fps: 3.442101548587232
time: 0.3108954429626465s, fps: 3.216515464075645
time: 0.29480838775634766s, fps: 3.392033746429484
time: 0.2799801826477051s, fps: 3.5716813616708194
time: 0.2937746047973633s, fps: 3.4039701991592137
time: 0.26947975158691406s, fps: 3.7108539476943765
time: 0.29370880126953125s, fps: 3.4047328363249085
time: 0.294309139251709s, fps: 3.3977877905610887
time: 0.294952392578125s, fps: 3.3903776513191928
time: 0.32036852836608887s, fps: 3.121405230095786
time: 0.3079049587249756s, fps: 3.2477554247290055
time: 0.32831406593322754s, fps: 3.0458640179107643
time: 0.3125002384185791s, fps: 3.1999975585956126
time: 0.310957670211792s, fps: 3.215871791549326
time: 0.29265475273132324s, fps: 3.41699559179231
time: 0.29028868675231934s, fps: 3.4448466152358943
time: 0.2930140495300293s, fps: 3.4128056371492037
time: 0.2709319591522217s, fps: 3.6909636025558554
time: 0.2918202877044678s, fps: 3.4267665482282026
time: 0.3565652370452881s, fps: 2.8045358776043217
time: 0.3324623107910156s, fps: 3.0078597409154018
time: 0.35688161849975586s, fps: 2.8020496101865895
time: 0.40174293518066406s, fps: 2.489153915177872
time: 0.3922445774078369s, fps: 2.549429762951824
time: 0.34782838821411133s, fps: 2.8749809787935825
time: 0.34010910987854004s, fps: 2.9402329163047725
time: 0.40003466606140137s, fps: 2.4997833558917364
time: 0.4031689167022705s, fps: 2.480349944086769
time: 0.35930466651916504s, fps: 2.7831533881474395
time: 0.34941864013671875s, fps: 2.8618965479595624
time: 0.34392523765563965s, fps: 2.9076086617442862
time: 0.3546156883239746s, fps: 2.819954200916251
time: 0.3458824157714844s, fps: 2.891155937400051
time: 0.3480362892150879s, fps: 2.873263596319968
time: 0.3717842102050781s, fps: 2.6897323031776814
ffmpeg version 4.0 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (crosstool-NG fa8859cb)
  configuration: --prefix=/root/.pyenv/versions/miniconda3-latest/envs/py373 --cc=/opt/conda/conda-bld/ffmpeg_1531088893642/_build_env/bin/x86_64-conda_cos6-linux-gnu-cc --disable-doc --enable-shared --enable-static --enable-zlib --enable-pic --enable-gpl --enable-version3 --disable-nonfree --enable-hardcoded-tables --enable-avresample --enable-libfreetype --disable-openssl --disable-gnutls --enable-libvpx --enable-pthreads --enable-libopus --enable-postproc --disable-libx264
  libavutil      56. 14.100 / 56. 14.100
  libavcodec     58. 18.100 / 58. 18.100
  libavformat    58. 12.100 / 58. 12.100
  libavdevice    58.  3.100 / 58.  3.100
  libavfilter     7. 16.100 /  7. 16.100
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  1.100 /  5.  1.100
  libswresample   3.  1.100 /  3.  1.100
  libpostproc    55.  1.100 / 55.  1.100
Input #0, avi, from 'output.avi':
  Metadata:
    encoder         : Lavf58.31.101
  Duration: 00:00:20.00, start: 0.000000, bitrate: 39103 kb/s
    Stream #0:0: Video: mjpeg (MJPG / 0x47504A4D), yuvj420p(pc, bt470bg/unknown/unknown), 1920x1080, 39167 kb/s, 25 fps, 25 tbr, 25 tbn, 25 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (mjpeg (native) -> mpeg4 (native))
Press [q] to stop, [?] for help
[swscaler @ 0x55db47af7780] deprecated pixel format used, make sure you did set range correctly
Output #0, mp4, to 'output.mp4':
  Metadata:
    encoder         : Lavf58.12.100
    Stream #0:0: Video: mpeg4 (mp4v / 0x7634706D), yuv420p, 1920x1080, q=2-31, 200 kb/s, 25 fps, 12800 tbn, 25 tbc
    Metadata:
      encoder         : Lavc58.18.100 mpeg4
    Side data:
      cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
frame=  500 fps= 90 q=31.0 Lsize=    3879kB time=00:00:19.96 bitrate=1591.9kbits/s speed=3.58x    
video:3876kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.077428%
#show_local_mp4_video('output.mp4', width=960, height=720)
from IPython.display import Video

Video("https://eigo.rumisunheart.com/wp-content/uploads/2019/09/output.mp4", width=800, height=600)

既存のコードを転用することで、非常に簡単に人物認識が可能なので驚かされる。

スポンサーリンク

yolo3による物体認識(画像編)

import os
from os.path import exists, join, basename, splitext

git_repo_url = 'https://github.com/ayooshkathuria/pytorch-yolo-v3.git'
project_name = splitext(basename(git_repo_url))[0]
if not exists(project_name):
  # クローンして依存関係をインストール
  !git clone -q $git_repo_url
  #!cd $project_name && pip install -q -r requirement.txt
  
import sys
sys.path.append(project_name)
import time
import matplotlib
import matplotlib.pylab as plt
plt.rcParams["axes.grid"] = False
if not exists('yolov3.weights'):
  !wget -q https://drive.google.com/file/d/1I92QB0ifKoEOZcUq6gAfay-o4Tjw1xTU
IMAGE_URL = 'https://i.postimg.cc/02pvdYwM/ancient-architecture-buildings-1583704.jpg'

image_file = basename(IMAGE_URL)
!wget -q -O $image_file $IMAGE_URL
plt.figure(figsize=(15, 15))
plt.imshow(matplotlib.image.imread(image_file))
<matplotlib.image.AxesImage at 0x7efebac2e490>
!cd pytorch-yolo-v3 && python detect.py --weights ../yolov3.weights --images ../$image_file --det ..

plt.figure(figsize=(15, 15))
plt.imshow(matplotlib.image.imread('det_%s' % image_file))
Loading network.....
Network successfully loaded
ancient-architecture-buildings-1583704.jpg predicted in  0.321 seconds
Objects Detected:    person person person person person person person person person person person person person person car car truck clock
----------------------------------------------------------

SUMMARY
----------------------------------------------------------
Task                     : Time Taken (in seconds)

Reading addresses        : 0.044
Loading batch            : 0.361
Detection (1 images)     : 0.371
Output Processing        : 0.000
Drawing Boxes            : 0.303
Average time_per_img     : 1.055
----------------------------------------------------------
<matplotlib.image.AxesImage at 0x7efeba795390>
スポンサーリンク
スポンサーリンク