Revert "pygame is fine, cv2 can also do the trick (#79)" (#85)

This reverts commit e7f2f43331.
This commit is contained in:
George Hotz 2020-11-09 10:03:38 -08:00 committed by GitHub
parent e7f2f43331
commit 6b982621f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 10 deletions

View File

@ -171,21 +171,24 @@ if __name__ == "__main__":
from PIL import Image
url = sys.argv[1]
if url == 'webcam':
import pygame
pygame.init()
SCALE = 3
screen = pygame.display.set_mode((224*SCALE, 224*SCALE))
pygame.display.set_caption("capture")
import cv2
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
while 1:
_ = cap.grab() # discard one frame to circumvent capture buffering
ret, frame = cap.read()
img = Image.fromarray(frame[:, :, [2,1,0]])
out, retimg = infer(model, img)
frame = Image.fromarray(frame[:, :, [2,1,0]])
out, retimg = infer(model, frame)
simg = cv2.resize(retimg, (224*SCALE, 224*SCALE))
pygame.surfarray.blit_array(screen, np.array(simg).swapaxes(0,1))
pygame.display.update()
for e in pygame.event.get():
pass
print(np.argmax(out.data), np.max(out.data), lbls[np.argmax(out.data)])
retimg = cv2.cvtColor(retimg, cv2.COLOR_RGB2BGR)
cv2.imshow('capture', retimg)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
else:
img = Image.open(io.BytesIO(fetch(url)))
st = time.time()