1
+ from _testbuffer import ndarray
2
+
1
3
import cv2
2
4
from neuralNetwork import Net2 , Net
3
5
import openCvTranforms .opencv_transforms .transforms as tf
4
6
import torchvision .transforms as transforms
5
- from timeit import default_timer as timer
6
7
import torch
7
- import numpy as np
8
+ from PyQt5 .QtCore import *
9
+ from time import time
8
10
9
11
# functions to show an image
12
+ class Signals (QObject ):
13
+ '''
14
+ Defines the signals available from a running worker thread.
15
+
16
+ Supported signals are:
17
+
18
+ finished
19
+ No data
20
+
21
+ error
22
+ `tuple` (exctype, value, traceback.format_exc() )
23
+
24
+ result
25
+ `object` data returned from processing, anything
26
+
27
+ '''
28
+ finished = pyqtSignal ()
29
+ error = pyqtSignal (tuple )
30
+ result = pyqtSignal (object , torch .Tensor )
31
+
32
+
33
+ class GestureRecognition (QThread ):
34
+ """main class with neural network"""
35
+
36
+ def __init__ (self , print ):
37
+ super (GestureRecognition , self ).__init__ ()
38
+ self .width = 80
39
+ self .heigh = 60
40
+ self .colors = [(255 , 0 , 0 ), (0 , 255 , 0 ), (0 , 0 , 255 )]
41
+ self .labels = ['0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' , '12' , '13' , '14' , '15' , '16' , '17' ,
42
+ '18' , '19' ,
43
+ '20' , '21' , '22' , '23' , '24' , '25' , '26' ]
44
+ self .maxLen = 200
45
+ self .step = 20
46
+ self .offset = 5
47
+ self .printFps = print
48
+ self .thicc = 10
49
+ self .device = cv2 .VideoCapture (0 , cv2 .CAP_DSHOW )
50
+ self .net = Net (self .width , self .heigh )
51
+ self .transform = transforms .Compose (
52
+ [tf .Grayscale (),
53
+ tf .Resize ((self .heigh , self .width )),
54
+ tf .ToTensor (),
55
+ tf .Normalize ((0.5 ,), (0.5 ,))
56
+ ])
57
+ self .signals = Signals ()
58
+ self .running = True
59
+ self .signals .finished .connect (self .stop )
60
+
61
+ def stop (self ):
62
+ self .running = False
63
+
64
+ def drawStatistics (self , image , stats , chosen ):
65
+ for i , data in enumerate (stats .view (- 1 )):
66
+ imm = 10
67
+ cv2 .rectangle (image , (self .offset , i * self .step ),
68
+ (self .offset + (data * self .maxLen ), (i * self .step ) + self .thicc ), self .colors [i % 3 ], - 1 )
69
+ cv2 .putText (image , self .labels [i ], (self .offset , i * self .step + 15 ), fontFace = cv2 .FONT_HERSHEY_SIMPLEX ,
70
+ fontScale = 0.5 ,
71
+ color = (255 , 255 , 255 ), thickness = 1 , lineType = - 1 )
72
+ nnm = 20
73
+
74
+ def imshow (self , img ):
75
+ img = img .view (self .heigh , self .width , 1 ).numpy () / 2 + 0.5 # unnormalize
76
+ print (img .shape )
77
+ # npimg = img.numpy()
78
+ # cv2.imshow("preview", img)
79
+
80
+ def run (self ):
81
+ scale = 2.5
82
+ # width = vc.get(3) # 640
83
+ # heigh = vc.get(4) # 480
84
+ # fps = vc.get(5) # 60
85
+
86
+ # new settings
87
+
88
+ self .device .set (5 , 25 ) # set fps
89
+ #print(self.device.get(16))
90
+ #self.device.set(16, 1) #set to rgb
91
+
92
+ #print("width: " + str(self.device.get(3)) + " heigh: " + str(self.device.get(4)))
93
+
94
+ self .net = torch .load ("./savedMode2.pth" )
95
+ self .net .eval ()
10
96
11
- width = 80
12
- heigh = 60
13
- colors = [(255 , 0 , 0 ), (0 , 255 , 0 ), (0 , 0 , 255 )]
14
- labels = ['0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , '10' , '11' , '12' , '13' , '14' , '15' , '16' , '17' , '18' , '19' ,
15
- '20' , '21' , '22' , '23' , '24' , '25' , '26' ];
16
- maxLen = 200
17
- step = 20
18
- offset = 5
19
- thicc = 10
20
-
21
-
22
- def drawStatistics (image , stats , chosen ):
23
- for i , data in enumerate (stats .view (- 1 )):
24
- imm = 10
25
- cv2 .rectangle (image , (offset , i * step ), (offset + (data * maxLen ), (i * step ) + thicc ), colors [i % 3 ], - 1 )
26
- cv2 .putText (image , labels [i ], (offset , i * step + 15 ), fontFace = cv2 .FONT_HERSHEY_SIMPLEX , fontScale = 0.5 ,
27
- color = (255 , 255 , 255 ), thickness = 1 , lineType = - 1 )
28
- nnm = 20
29
-
30
-
31
- def imshow (img ):
32
- img = img .view (heigh , width , 1 ).numpy () / 2 + 0.5 # unnormalize
33
- print (img .shape )
34
- # npimg = img.numpy()
35
- cv2 .imshow ("preview" , img )
36
-
37
-
38
- cv2 .namedWindow ("preview" )
39
- vc = cv2 .VideoCapture (0 , cv2 .CAP_DSHOW )
40
- scale = 2.5
41
- # width = vc.get(3) # 640
42
- # heigh = vc.get(4) # 480
43
- # fps = vc.get(5) # 60
44
-
45
- # new settings
46
-
47
- vc .set (5 , 24 ) # set fps
48
-
49
- print ("width: " + str (vc .get (3 )) + " heigh: " + str (vc .get (4 )))
50
- net = Net (width , heigh )
51
- net = torch .load ("./savedMode2.pth" )
52
- net .eval ()
53
-
54
- transform = transforms .Compose (
55
- [tf .Grayscale (),
56
- tf .Scale ((heigh , width )),
57
- tf .ToTensor (),
58
- tf .Normalize ((0.5 ,), (0.5 ,))
59
- ])
60
-
61
- if vc .isOpened (): # try to get the first frame
62
- rval , frame = vc .read ()
63
- print (frame .shape )
64
- else :
65
- rval = False
66
- out = torch .zeros (27 )
67
- while rval :
68
- start = timer ()
69
- rval , frame = vc .read ()
70
- # frame = cv2.imread(r'./kinect_leap_dataset/acquisitions/P1/G1/0_rgb.png')
71
- # print(frame.shape)
72
- frameWithStats = frame .copy ()
73
- drawStatistics (frameWithStats , out , 1 )
74
- cv2 .imshow ("preview" , frameWithStats )
75
- frame = transform (frame ).view (- 1 , 1 , heigh , width )
76
- print (frame .shape )
77
- # imshow(frame)
78
- out = net (frame )
79
- print (out .shape )
80
- end = timer ()
81
- print (end - start )
82
- key = cv2 .waitKey (1 )
83
- if key == 27 : # exit on ESC
84
- break # todo change to kayboard real time check
85
- cv2 .destroyWindow ("preview" )
97
+ if self .device .isOpened (): # try to get the first frame
98
+ rval , frame = self .device .read ()
99
+ # print(frame.shape)
100
+ else :
101
+ rval = False
102
+ out = torch .zeros (27 )
103
+ start = time ()
104
+ fps = 0
105
+ while rval and self .running :
106
+ if time () - start < 1 :
107
+ fps += 1
108
+ else :
109
+ start = time ()
110
+ self .printFps (fps )
111
+ fps = 0
112
+ rval , frame = self .device .read ()
113
+ # frame = cv2.imread(r'./kinect_leap_dataset/acquisitions/P1/G1/0_rgb.png')
114
+ frame = cv2 .cvtColor (frame , cv2 .COLOR_BGR2RGB )
115
+ frameWithStats = frame .copy ()
116
+ #print(frameWithStats.shape)
117
+ self .drawStatistics (frameWithStats , out , 1 )
118
+ # cv2.imshow("preview", frameWithStats)
119
+ frame = self .transform (frame ).view (- 1 , 1 , self .heigh , self .width )
120
+ #print(frame.shape)
121
+ #imshow(frame)
122
+ out = self .net (frame )
123
+ self .signals .result .emit (frameWithStats , out )
124
+ #print(out.shape)
0 commit comments