Skip to content

Commit db95ad1

Browse files
author
Roberto De Ioris
committed
added code for EuroPython 2017 upcoming demo
1 parent 03c2f96 commit db95ad1

File tree

10 files changed

+303
-0
lines changed

10 files changed

+303
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unreal_engine as ue
2+
3+
from unreal_engine.classes import SceneCaptureComponent2D, PyHUD
4+
from unreal_engine.enums import ESceneCaptureSource
5+
6+
import os
7+
8+
import cv2
9+
import numpy
10+
11+
class Sight:
12+
13+
def __init__(self):
14+
self.what_i_am_seeing = ue.create_transient_texture_render_target2d(512, 512)
15+
16+
def pre_initialize_components(self):
17+
# add a new root component (a SceneCaptureComponent2D one)
18+
self.scene_capturer = self.uobject.add_actor_root_component(SceneCaptureComponent2D, 'Scene Capture')
19+
# use the previously created texture as the render target
20+
self.scene_capturer.TextureTarget = self.what_i_am_seeing
21+
# store pixels as linear colors (non HDR)
22+
self.scene_capturer.CaptureSource = ESceneCaptureSource.SCS_FinalColorLDR
23+
24+
def begin_play(self):
25+
# get a reference to the pawn currently controlled by the player
26+
mannequin = self.uobject.get_player_pawn()
27+
# attach myself to the 'head' bone of the mannequin Mesh component
28+
self.uobject.attach_to_component(mannequin.Mesh, 'head')
29+
30+
# spawn a new HUD (well, a PyHUD)
31+
hud = self.uobject.actor_spawn(PyHUD, PythonModule='hud', PythonClass='FacesDetector')
32+
# get a reference to its proxy class
33+
self.py_hud = hud.get_py_proxy()
34+
35+
# set the texture to draw
36+
self.py_hud.texture_to_draw = self.what_i_am_seeing
37+
38+
# use this new HUD as the player default one (so the engine will start drawing it)
39+
self.uobject.set_player_hud(hud)
40+
41+
def tick(self, delta_time):
42+
# read raw data from the render target
43+
data = self.what_i_am_seeing.render_target_get_data()
44+
# convert them to a numpy array with the format expected by OpenCV
45+
npy_data = numpy.frombuffer(data, dtype=numpy.uint8).reshape(512, 512, 4)
46+
self.py_hud.gray_data = cv2.cvtColor(npy_data, cv2.COLOR_BGRA2GRAY)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import unreal_engine as ue
2+
3+
from unreal_engine.classes import SceneCaptureComponent2D, PyHUD
4+
from unreal_engine.enums import ESceneCaptureSource
5+
6+
import cv2
7+
import numpy
8+
9+
class Sight:
10+
11+
def __init__(self):
12+
self.what_i_am_seeing = ue.create_transient_texture_render_target2d(512, 512)
13+
14+
def pre_initialize_components(self):
15+
# add a new root component (a SceneCaptureComponent2D one)
16+
self.scene_capturer = self.uobject.add_actor_root_component(SceneCaptureComponent2D, 'Scene Capture')
17+
# use the previously created texture as the render target
18+
self.scene_capturer.TextureTarget = self.what_i_am_seeing
19+
# store pixels as linear colors (non HDR)
20+
self.scene_capturer.CaptureSource = ESceneCaptureSource.SCS_FinalColorLDR
21+
22+
def begin_play(self):
23+
# get a reference to the pawn currently controlled by the player
24+
mannequin = self.uobject.get_player_pawn()
25+
# attach myself to the 'head' bone of the mannequin Mesh component
26+
self.uobject.attach_to_component(mannequin.Mesh, 'head')
27+
28+
# spawn a new HUD (well, a PyHUD)
29+
hud = self.uobject.actor_spawn(PyHUD, PythonModule='hud_second', PythonClass='FacesDetector')
30+
# get a reference to its proxy class
31+
self.py_hud = hud.get_py_proxy()
32+
33+
# set the texture to draw
34+
self.py_hud.texture_to_draw = self.what_i_am_seeing
35+
36+
# use this new HUD as the player default one (so the engine will start drawing it)
37+
self.uobject.set_player_hud(hud)
38+
39+
def tick(self, delta_time):
40+
# read raw data from the render target
41+
data = self.what_i_am_seeing.render_target_get_data()
42+
# convert them to a numpy array with the format expected by OpenCV
43+
npy_data = numpy.frombuffer(data, dtype=numpy.uint8).reshape(512, 512, 4)
44+
# pass data to the HUD
45+
self.py_hud.gray_data = cv2.cvtColor(npy_data, cv2.COLOR_BGRA2GRAY)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unreal_engine as ue
2+
3+
4+
class Sight:
5+
6+
def __init__(self):
7+
ue.log_warning('Hello i am __init__')
8+
self.timer = 1.0
9+
10+
def begin_play(self):
11+
ue.log_warning('Hello i am begin_play')
12+
13+
def tick(self, delta_time):
14+
self.timer -= delta_time
15+
if self.timer <= 0:
16+
ue.log_error('1 second elapsed !')
17+
self.timer = 1.0
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import unreal_engine as ue
2+
3+
from unreal_engine.classes import SceneCaptureComponent2D, PyHUD
4+
from unreal_engine.enums import ESceneCaptureSource
5+
6+
class Sight:
7+
8+
def __init__(self):
9+
self.what_i_am_seeing = ue.create_transient_texture_render_target2d(512, 512)
10+
11+
def pre_initialize_components(self):
12+
# add a new root component (a SceneCaptureComponent2D one)
13+
self.scene_capturer = self.uobject.add_actor_root_component(SceneCaptureComponent2D, 'Scene Capture')
14+
# use the previously created texture as the render target
15+
self.scene_capturer.TextureTarget = self.what_i_am_seeing
16+
# store pixels as linear colors (non HDR)
17+
self.scene_capturer.CaptureSource = ESceneCaptureSource.SCS_FinalColorLDR
18+
19+
def begin_play(self):
20+
# get a reference to the pawn currently controlled by the player
21+
mannequin = self.uobject.get_player_pawn()
22+
# attach myself to the 'head' bone of the mannequin Mesh component
23+
self.uobject.attach_to_component(mannequin.Mesh, 'head')
24+
25+
# spawn a new HUD (well, a PyHUD)
26+
hud = self.uobject.actor_spawn(PyHUD, PythonModule='hud_first', PythonClass='FacesDetector')
27+
# get a reference to its proxy class
28+
self.py_hud = hud.get_py_proxy()
29+
30+
# set the texture to draw
31+
self.py_hud.texture_to_draw = self.what_i_am_seeing
32+
33+
# use this new HUD as the player default one (so the engine will start drawing it)
34+
self.uobject.set_player_hud(hud)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unreal_engine as ue
2+
3+
from unreal_engine.classes import SceneCaptureComponent2D
4+
from unreal_engine.enums import ESceneCaptureSource
5+
6+
class Sight:
7+
8+
def __init__(self):
9+
self.what_i_am_seeing = ue.create_transient_texture_render_target2d(512, 512)
10+
11+
def pre_initialize_components(self):
12+
# add a new root component (a SceneCaptureComponent2D one)
13+
self.scene_capturer = self.uobject.add_actor_root_component(SceneCaptureComponent2D, 'Scene Capture')
14+
# use the previously created texture as the render target
15+
self.scene_capturer.TextureTarget = self.what_i_am_seeing
16+
# store pixels as linear colors (non HDR)
17+
self.scene_capturer.CaptureSource = ESceneCaptureSource.SCS_FinalColorLDR
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unreal_engine as ue
2+
3+
from unreal_engine.classes import SceneCaptureComponent2D
4+
from unreal_engine.enums import ESceneCaptureSource
5+
6+
class Sight:
7+
8+
def __init__(self):
9+
self.what_i_am_seeing = ue.create_transient_texture_render_target2d(512, 512)
10+
11+
def pre_initialize_components(self):
12+
# add a new root component (a SceneCaptureComponent2D one)
13+
self.scene_capturer = self.uobject.add_actor_root_component(SceneCaptureComponent2D, 'Scene Capture')
14+
# use the previously created texture as the render target
15+
self.scene_capturer.TextureTarget = self.what_i_am_seeing
16+
# store pixels as linear colors (non HDR)
17+
self.scene_capturer.CaptureSource = ESceneCaptureSource.SCS_FinalColorLDR
18+
19+
def begin_play(self):
20+
# get a reference to the pawn currently controlled by the player
21+
mannequin = self.uobject.get_player_pawn()
22+
# attach myself to the 'head' bone of the mannequin Mesh component
23+
self.uobject.attach_to_component(mannequin.Mesh, 'head')
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import unreal_engine as ue
2+
3+
from unreal_engine import FColor, FLinearColor
4+
5+
from unreal_engine.enums import EBlendMode, EPixelFormat
6+
7+
8+
import os
9+
import cv2
10+
11+
class FacesDetector:
12+
13+
def __init__(self):
14+
self.texture_to_draw = None
15+
self.gray_data = None
16+
17+
self.texture = ue.create_transient_texture(512, 512, EPixelFormat.PF_G8)
18+
self.texture.auto_root()
19+
filename = os.path.join(ue.get_content_dir(), 'haarcascade_frontalface_default.xml')
20+
self.cascade = cv2.CascadeClassifier(filename)
21+
22+
def draw_hud(self):
23+
# exit if we do not have enough data
24+
if not self.texture_to_draw:
25+
return
26+
27+
# upload grayscale data into the GPU
28+
self.texture.texture_set_data(self.gray_data)
29+
30+
# draw what the player pawn is seeing
31+
self.uobject.hud_draw_texture(self.texture_to_draw, 0, 0, 256, 256)
32+
33+
# draw in opaque mode (ignore transparency)
34+
self.uobject.hud_draw_texture(self.texture, 0, 256, 256, 256, 0, 0, 1, 1, FLinearColor(1, 1, 1, 1), EBlendMode.BLEND_Opaque)
35+
36+
# detect faces
37+
faces = self.cascade.detectMultiScale(self.gray_data)
38+
# highlight detected areas
39+
for x, y, w, h in faces:
40+
self.uobject.hud_draw_rect(x/2.0, 256 + y/2.0, w/2.0, h/2.0, FLinearColor(0, 1, 0, 0.5))
41+
# report the number of detected faces
42+
self.uobject.hud_draw_text('I am seeing {0} faces'.format(len(faces)), 500, 50, FLinearColor(0, 1, 1, 1), 2)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unreal_engine as ue
2+
3+
class FacesDetector:
4+
5+
def __init__(self):
6+
self.texture_to_draw = None
7+
8+
def draw_hud(self):
9+
# exit if we do not have enough data
10+
if not self.texture_to_draw:
11+
return
12+
13+
# draw what the player pawn is seeing
14+
self.uobject.hud_draw_texture(self.texture_to_draw, 0, 0, 256, 256)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import unreal_engine as ue
2+
3+
from unreal_engine import FColor, FLinearColor
4+
5+
from unreal_engine.enums import EBlendMode, EPixelFormat
6+
7+
8+
import os
9+
import cv2
10+
11+
class FacesDetector:
12+
13+
def __init__(self):
14+
self.texture_to_draw = None
15+
self.gray_data = None
16+
17+
# create a transient texture with a single color channel
18+
self.texture = ue.create_transient_texture(512, 512, EPixelFormat.PF_G8)
19+
# this automatically destroys the texture when self.texture dies (this is required as UE does not know about this texture object)
20+
self.texture.auto_root()
21+
22+
def draw_hud(self):
23+
# exit if we do not have enough data
24+
if not self.texture_to_draw:
25+
return
26+
27+
# upload grayscale data into the GPU
28+
self.texture.texture_set_data(self.gray_data)
29+
30+
# draw what the player pawn is seeing
31+
self.uobject.hud_draw_texture(self.texture_to_draw, 0, 0, 256, 256)
32+
33+
# draw in opaque mode (ignore transparency)
34+
self.uobject.hud_draw_texture(self.texture, 0, 256, 256, 256, 0, 0, 1, 1, FLinearColor(1, 1, 1, 1), EBlendMode.BLEND_Opaque)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unreal_engine as ue
2+
3+
from unreal_engine.classes import Material
4+
from unreal_engine.enums import EPixelFormat
5+
6+
import cv2
7+
8+
class WebCam:
9+
10+
def begin_play(self):
11+
# open the first video capture device
12+
self.capture = cv2.VideoCapture(0)
13+
# get its size
14+
width = int(self.capture.get(cv2.CAP_PROP_FRAME_WIDTH))
15+
height = int(self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
16+
# create a new texture with the desired size
17+
self.texture = ue.create_transient_texture(width, height, EPixelFormat.PF_R8G8B8A8)
18+
19+
# assign the texture to the material mapped to the cube
20+
self.material = self.uobject.create_material_instance_dynamic(ue.load_object(Material, '/Game/DumbMaterial'))
21+
self.uobject.get_owner().StaticMeshComponent.set_material(0, self.material)
22+
self.material.set_material_texture_parameter('Texture', self.texture)
23+
24+
def tick(self, delta_time):
25+
# get webcam frame
26+
ret, frame = self.capture.read()
27+
if not ret:
28+
return
29+
# frames are in plain RGB, convert them to a texture-useful format
30+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
31+
self.texture.texture_set_data(frame)

0 commit comments

Comments
 (0)