Skip to content

Commit dd22836

Browse files
committed
Testing with vispy and camera
Just testing again
1 parent a3d1c13 commit dd22836

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

camera_v2.py

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
# -*- coding: utf-8 -*-
2+
# vispy: testskip
3+
# -----------------------------------------------------------------------------
4+
# Copyright (c) 2015, Vispy Development Team.
5+
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
6+
# -----------------------------------------------------------------------------
7+
"""Display a live webcam feed. Require OpenCV (Python 2 only).
8+
"""
9+
10+
try:
11+
import cv2
12+
except Exception:
13+
raise ImportError("You need OpenCV for this example.")
14+
15+
import numpy as np
16+
from vispy import app
17+
from vispy import gloo
18+
19+
import random
20+
from ctypes import *
21+
import time
22+
23+
PLATFORM_SUFFIX = "64" if sizeof(c_voidp) == 8 else ""
24+
VERSION = 0x00011000 #CHANGE THIS TO MATCH CORRECT VERSION!!!
25+
BANK_FILES = [ "Master Bank.bank", "Master Bank.strings.bank", "Weapons.bank","Vehicles.bank","UI_Menu.bank","Surround_Ambience.bank","Music.bank"]
26+
27+
studio_dll = None
28+
studio_sys = None
29+
30+
def check_result(r):
31+
if r != 0:
32+
print("ERROR: Got FMOD_RESULT {0}".format(r))
33+
34+
35+
def studio_init():
36+
print("Initializing FMOD Studio");
37+
global studio_dll
38+
global studio_sys
39+
studio_dll = WinDLL("fmodstudioL" + PLATFORM_SUFFIX)
40+
41+
lowlevel_dll = WinDLL("fmodL" + PLATFORM_SUFFIX)
42+
# Write debug log to file
43+
44+
check_result(lowlevel_dll.FMOD_Debug_Initialize(0x00000002, 1, 0, "log.txt".encode('ascii')))
45+
# Create studio system
46+
studio_sys = c_voidp()
47+
check_result(studio_dll.FMOD_Studio_System_Create(byref(studio_sys), VERSION))
48+
# Call System init
49+
#print ("ok")
50+
check_result(studio_dll.FMOD_Studio_System_Initialize(studio_sys, 256, 0, 0, c_voidp()))
51+
# Load banks
52+
53+
for bankname in BANK_FILES:
54+
print("Loading bank: " + bankname)
55+
bank = c_voidp()
56+
check_result(studio_dll.FMOD_Studio_System_LoadBankFile(studio_sys, bankname.encode('ascii'), 0, byref(bank)))
57+
58+
59+
60+
def play_sound(soundname,time=0):
61+
print("Playing sound: " + soundname)
62+
event_desc = c_voidp()
63+
check_result(studio_dll.FMOD_Studio_System_GetEvent(studio_sys, soundname.encode('ascii'), byref(event_desc)))
64+
event_inst = c_voidp()
65+
check_result(studio_dll.FMOD_Studio_EventDescription_CreateInstance(event_desc, byref(event_inst)))
66+
check_result(studio_dll.FMOD_Studio_EventInstance_SetVolume(event_inst, c_float(0.75+.2*random.random())))
67+
68+
69+
check_result(studio_dll.FMOD_Studio_EventInstance_SetPitch(event_inst,c_float(.9+.2*random.random())))
70+
check_result(studio_dll.FMOD_Studio_EventInstance_SetReverbLevel( event_inst, c_int(3),c_float(1.8)))
71+
72+
check_result(studio_dll.FMOD_Studio_EventInstance_SetTimelinePosition(event_inst,c_int(time)));
73+
74+
check_result(studio_dll.FMOD_Studio_EventInstance_Start(event_inst))
75+
check_result(studio_dll.FMOD_Studio_EventInstance_Release(event_inst))
76+
77+
78+
79+
80+
81+
vertex = """
82+
attribute vec2 position;
83+
attribute vec2 texcoord;
84+
varying vec2 v_texcoord;
85+
void main()
86+
{
87+
gl_Position = vec4(position, 0.0, 1.0);
88+
v_texcoord = texcoord;
89+
}
90+
"""
91+
92+
fragment = """
93+
uniform sampler2D texture;
94+
95+
uniform sampler2D texture2;
96+
varying vec2 v_texcoord;
97+
98+
99+
100+
void main()
101+
{
102+
vec2 uv=v_texcoord-.5;
103+
gl_FragColor.b= .5-.5*(sqrt(uv.x*uv.x+uv.y*uv.y)); //background blue
104+
105+
//gl_FragColor.r= 55.*(.5-(sqrt(uv.x*uv.x+uv.y*uv.y))); //background red
106+
107+
108+
if ((v_texcoord.y>0.875)&&(v_texcoord.x<0.125))
109+
{gl_FragColor = float(v_texcoord.y>0.875)*float(v_texcoord.x<0.125)*texture2D(texture, fract(v_texcoord*8.)); //cam pic
110+
gl_FragColor =gl_FragColor.bgra;}
111+
112+
if ((v_texcoord.y<0.125)&&(v_texcoord.x<0.125))
113+
gl_FragColor = float(v_texcoord.y<0.125)*float(v_texcoord.x<0.125)*texture2D(texture2, fract(v_texcoord*8.)); //rand pic
114+
115+
if(1>0)
116+
{if (v_texcoord.y>0.875)if (v_texcoord.x>0.125)//blue area
117+
gl_FragColor= vec4(0,0,0,1);
118+
119+
if (v_texcoord.y<0.125)if (v_texcoord.x>0.125)//blue area
120+
gl_FragColor= vec4(0,0,0,1); }
121+
122+
123+
//gl_FragColor+=vec4(v_texcoord,0,0);
124+
}
125+
"""
126+
127+
128+
class Canvas(app.Canvas):
129+
def __init__(self):
130+
app.Canvas.__init__(self, size=(1280, 960), keys='interactive')
131+
self.program = gloo.Program(vertex, fragment, count=4)
132+
self.program['position'] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)]
133+
self.program['texcoord'] = [(1, 1), (1, 0), (0, 1), (0, 0)]
134+
self.program['texture2'] = rtex=(1024*np.random.rand(480, 640, 3)).astype(np.uint8)#random texture
135+
self.program['texture'] = np.zeros((480, 640, 3)).astype(np.uint8)#free space for cam texture
136+
137+
#print (self.program['texture2'])
138+
#print (self.program['texture'])
139+
#print (rtex)
140+
141+
width, height = self.physical_size
142+
gloo.set_viewport(0, 0, width, height)
143+
144+
self.cap = cv2.VideoCapture(0)
145+
if not self.cap.isOpened():
146+
raise Exception("There's no available camera.")
147+
self._timer = app.Timer('auto', connect=self.on_timer, start=True)
148+
149+
self.show()
150+
151+
def on_resize(self, event):
152+
width, height = event.physical_size
153+
gloo.set_viewport(0, 0, width, height)
154+
play_sound("event:/UI/Cancel",0)
155+
check_result(studio_dll.FMOD_Studio_System_Update(studio_sys))
156+
157+
158+
159+
def on_draw(self, event):
160+
gloo.clear('black')
161+
_, im = self.cap.read()
162+
self.program['texture'][...] = im
163+
self.program.draw('triangle_strip')
164+
check_result(studio_dll.FMOD_Studio_System_Update(studio_sys))
165+
166+
def on_timer(self, event):
167+
self.update()
168+
169+
170+
studio_init()
171+
c = Canvas()
172+
play_sound("event:/Ambience/Country",33000)
173+
app.run()
174+
c.cap.release()
175+
#play_sound("event:/Music/Music")
176+
#tick_update(5)
177+
#play_sound("event:/Explosions/Single Explosion")
178+
#tick_update()

0 commit comments

Comments
 (0)