-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathspout_webcam_sender_example.py
122 lines (93 loc) · 3.82 KB
/
spout_webcam_sender_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# Add relative directory ../Library to import path, so we can import the SpoutSDK.pyd library. Feel free to remove these if you put the SpoutSDK.pyd file in the same directory as the python scripts.
import sys
sys.path.append('../Library')
import argparse
import cv2
import SpoutSDK
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
"""parsing and configuration"""
def parse_args():
desc = "Spout for Python webcam sender example"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--camSize', nargs = 2, type=int, default=[640, 480], help='File path of content image (notation in the paper : x)')
parser.add_argument('--camID', type=int, default=1, help='Webcam Device ID)')
return parser.parse_args()
"""main"""
def main():
# parse arguments
args = parse_args()
# window details
width = args.camSize[0]
height = args.camSize[1]
display = (width,height)
# window setup
pygame.init()
pygame.display.set_caption('Spout for Python Webcam Sender Example')
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8)
# init capture & set size
cap = cv2.VideoCapture(args.camID)
cap.set(3, width)
cap.set(4, height)
# OpenGL init
glMatrixMode(GL_PROJECTION)
glOrtho(0,width,height,0,1,-1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glDisable(GL_DEPTH_TEST)
glClearColor(0.0,0.0,0.0,0.0)
glEnable(GL_TEXTURE_2D)
# init spout sender
spoutSender = SpoutSDK.SpoutSender()
spoutSenderWidth = width
spoutSenderHeight = height
# Its signature in c++ looks like this: bool CreateSender(const char *Sendername, unsigned int width, unsigned int height, DWORD dwFormat = 0);
spoutSender.CreateSender('Spout for Python Webcam Sender Example', width, height, 0)
# create texture id for use with Spout
senderTextureID = glGenTextures(1)
# initalise our sender texture
glBindTexture(GL_TEXTURE_2D, senderTextureID)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glBindTexture(GL_TEXTURE_2D, 0)
# loop
while(True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
ret, frame = cap.read()
frame = cv2.flip(frame, 1 )
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# Copy the frame from the webcam into the sender texture
glBindTexture(GL_TEXTURE_2D, senderTextureID)
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, frame )
# Send texture to Spout
# Its signature in C++ looks like this: bool SendTexture(GLuint TextureID, GLuint TextureTarget, unsigned int width, unsigned int height, bool bInvert=true, GLuint HostFBO = 0);
spoutSender.SendTexture(senderTextureID, GL_TEXTURE_2D, spoutSenderWidth, spoutSenderHeight, False, 0)
# Clear screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
# reset the drawing perspective
glLoadIdentity()
# Draw texture to screen
glBegin(GL_QUADS)
glTexCoord(0,0)
glVertex2f(0,0)
glTexCoord(1,0)
glVertex2f(width,0)
glTexCoord(1,1)
glVertex2f(width,height)
glTexCoord(0,1)
glVertex2f(0,height)
glEnd()
# update window
pygame.display.flip()
# unbind our sender texture
glBindTexture(GL_TEXTURE_2D, 0)
if __name__ == '__main__':
main()