首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用摄像头作为pygame的屏幕?

问如何使用摄像头作为pygame的屏幕?
EN

Stack Overflow用户
提问于 2020-01-28 20:40:36
回答 1查看 306关注 0票数 1

我想从网络摄像头的blit图像到pygame的屏幕上。我使用的是OS。我知道pygame目前只支持Linux和v4l2摄像头,但是,我们如何在pygame中使用摄像头呢?

EN

回答 1

Stack Overflow用户

发布于 2020-06-16 18:33:03

代码语言:javascript
复制
import cv2
import pygame
import numpy as np

pygame.init()
pygame.display.set_caption("OpenCV camera stream on Pygame")
surface = pygame.display.set_mode([1280,720])
#0 Is the built in camera
cap = cv2.VideoCapture(0)
#Gets fps of your camera
fps = cap.get(cv2.CAP_PROP_FPS)
print("fps:", fps)
#If your camera can achieve 60 fps
#Else just have this be 1-30 fps
cap.set(cv2.CAP_PROP_FPS, 60)

while True:
    surface.fill([0,0,0])

    success, frame = cap.read()
    if not success:
        break

    #for some reasons the frames appeared inverted
    frame = np.fliplr(frame)
    frame = np.rot90(frame)

    # The video uses BGR colors and PyGame needs RGB
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    surf = pygame.surfarray.make_surface(frame)

    for event in pygame.event.get():
        if event.type == pygame.KEYUP:
            background_color = red
            surface.fill(background_color)
            pygame.display.update
            end_time = self.time()

    # Show the PyGame surface!
    surface.blit(surf, (0,0))
    pygame.display.flip()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59948996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档