角蜂鸟Demo要在图形界面下才能跑,有些场景做演示不合适,且浪费资源。最好能在命令行下跑。我用pygame,实现在命令行下运行,就可以看到视频的Demo。
- import numpy as np,sys,cv2
- sys.path.append('../../api/')
- import hsapi as hs
- WEBCAM = False # Set to True if use Webcam
- net = hs.HS('ObjectDetector', zoom = True, verbose = 2)
- if WEBCAM: video_capture = cv2.VideoCapture(0)
- import time, pygame
- from pygame.locals import *
- # 显示器分辨率,我用的是竖屏,所以是w=1080 h=1920
- screen_size=(1080,1920)
- def init():
- pygame.init()
- screen = pygame.display.set_mode(screen_size)
- return screen
- def update(screen,surface):
- screen.blit(surface,((screen_size[0]-surface.get_width())/2, (screen_size[1]-surface.get_height())/2))
- pygame.display.update()
- def eventCheck():
- for event in pygame.event.get():
- if event.type in [QUIT,KEYDOWN]:
- pygame.quit()
- sys.exit()
- screen=init()
- while True:
- if WEBCAM: _, img = video_capture.read()
- else: img = None
- result = net.run(img)
- img = net.plotSSD(result)
- img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
- s = img.tostring()
- p = pygame.image.frombuffer(s,(img.shape[1],img.shape[0]),"RGB")
- eventCheck()
- update(screen,p)
复制代码
作者:_49_
來源:简书
|