forked from pupil-labs/pyuvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uvc.pyx
1204 lines (986 loc) · 43.8 KB
/
uvc.pyx
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
(*)~----------------------------------------------------------------------------------
Pupil - eye tracking platform
Copyright (C) 2012-2015 Pupil Labs
Distributed under the terms of the CC BY-NC-SA License.
License details are in the file LICENSE, distributed as part of this software.
----------------------------------------------------------------------------------~(*)
'''
import cython
from libc.string cimport memset
cimport cuvc as uvc
cimport cturbojpeg as turbojpeg
cimport numpy as np
import numpy as np
from cuvc cimport uvc_frame_t, timeval
from av.packet import Packet
from av.codec.codec import Codec
from time import monotonic
IF UNAME_SYSNAME == "Windows":
include "windows_time.pxi"
ELIF UNAME_SYSNAME == "Darwin":
include "darwin_time.pxi"
ELIF UNAME_SYSNAME == "Linux":
include "linux_time.pxi"
uvc_error_codes = { 0:"Success (no error)",
-1:"Input/output error.",
-2:"Invalid parameter.",
-3:"Access denied.",
-4:"No such device.",
-5:"Entity not found.",
-6:"Resource busy.",
-7:"Operation timed out.",
-8:"Overflow.",
-9:"Pipe error.",
-10:"System call interrupted.",
-11:"Insufficient memory. ",
-12:"Operation not supported.",
-50:"Device is not UVC-compliant.",
-51:"Mode not supported.",
-52:"Resource has a callback (can't use polling and async)",
-99:"Undefined error."}
cdef dict _str_to_fmt_map = {'MJPEG': uvc.UVC_FRAME_FORMAT_MJPEG ,
'H264': uvc.UVC_FRAME_FORMAT_H264}
cdef dict _vsfmt_to_str_map = {uvc.UVC_VS_FRAME_MJPEG : 'MJPEG',
uvc.UVC_VS_FRAME_FRAME_BASED : 'H264'}
cdef uvc.uvc_frame_format _str_to_fmt(str fmt_str) except *:
return _str_to_fmt_map[fmt_str]
cdef str _fmt_to_str(int fmt):
return _vsfmt_to_str_map[fmt]
#return next(key for key, value in _str_to_fmt_map.items() if value == fmt)
cpdef enum uvc_subsampling:
YUV_422 = turbojpeg.TJSAMP_422
YUV_420 = turbojpeg.TJSAMP_420
cdef str _to_str(object s):
if type(s) is str:
return <str>s
else:
return (<bytes>s).decode('utf-8')
class CaptureError(Exception):
def __init__(self, message):
super(CaptureError, self).__init__()
self.message = message
class StreamError(CaptureError):
def __init__(self, message):
super(StreamError, self).__init__(message)
self.message = message
class InitError(CaptureError):
def __init__(self, message):
super(InitError, self).__init__(message)
self.message = message
class OpenError(InitError):
def __init__(self, message):
super(InitError, self).__init__(message)
self.message = message
class DeviceNotFoundError(InitError):
def __init__(self, message):
super(InitError, self).__init__(message)
self.message = message
#logging
import logging
logger = logging.getLogger(__name__)
__version__ = '0.13' #make sure this is the same in setup.py
cdef class Frame:
cdef:
str encoding
str col_fmt
object subsampling
bint _bgr_converted
uvc.uvc_frame * _uvc_frame
def __cinit__(self):
pass
def __init__(self, encoding='MJPEG', col_fmt='YUV', subsampling = (4,2,2)):
self.encoding =encoding
self.subsampling = subsampling
pass
cdef attach_uvcframe(self,uvc.uvc_frame *uvc_frame,copy=True):
pass
cdef yuv2bgr(self):
pass
property width:
def __get__(self):
pass
property height:
def __get__(self):
pass
property index:
def __get__(self):
pass
property raw_buffer:
def __get__(self):
pass
property yuv_buffer:
def __get__(self):
pass
property yuv420:
def __get__(self):
'''
planar YUV420 returned in 3 numpy arrays:
420 subsampling:
Y(height,width) U(height/2,width/2), V(height/2,width/2)
'''
cdef np.ndarray[np.uint8_t, ndim=2] Y,U,V
y_plane_len = self.width*self.height
Y = np.asarray(self._yuv_buffer[:y_plane_len]).reshape(self.height,self.width)
if self.subsampling == (4,2,2):
uv_plane_len = y_plane_len//2
offset = y_plane_len
U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width/2)
offset += uv_plane_len
V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width/2)
#hack solution to go from YUV422 to YUV420
U = U[::2,:]
V = V[::2,:]
elif self.subsampling == (4,2,0):
uv_plane_len = y_plane_len//4
offset = y_plane_len
U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height/2,self.width/2)
offset += uv_plane_len
V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height/2,self.width/2)
elif self.subsampling == (4,4,4):
uv_plane_len = y_plane_len
offset = y_plane_len
U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width)
offset += uv_plane_len
V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width)
#hack solution to go from YUV444 to YUV420
U = U[::2,::2]
V = V[::2,::2]
return Y,U,V
property yuv422:
def __get__(self):
'''
planar YUV420 returned in 3 numpy arrays:
422 subsampling:
Y(height,width) U(height,width/2), V(height,width/2)
'''
cdef np.ndarray[np.uint8_t, ndim=2] Y,U,V
y_plane_len = self.width*self.height
Y = np.asarray(self._yuv_buffer[:y_plane_len]).reshape(self.height,self.width)
if self.subsampling == (4,2,2):
uv_plane_len = y_plane_len//2
offset = y_plane_len
U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width//2)
offset += uv_plane_len
V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width//2)
elif self.subsampling == (4,2,0):
raise Exception("can not convert from YUV420 to YUV422")
elif self.subsampling == (4,4,4):
uv_plane_len = y_plane_len
offset = y_plane_len
U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width)
offset += uv_plane_len
V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width)
#hack solution to go from YUV444 to YUV420
U = U[:,::2]
V = V[:,::2]
return Y,U,V
property gray:
def __get__(self):
# return gray aka luminace plane of YUV image.
cdef np.ndarray[np.uint8_t, ndim=2] Y
Y = np.asarray(self._yuv_buffer[:self.width*self.height]).reshape(self.height,self.width)
return Y
property bgr:
def __get__(self):
if self._bgr_converted is False:
self.yuv2bgr()
cdef np.ndarray[np.uint8_t, ndim=3] BGR
BGR = np.asarray(self._bgr_buffer).reshape(self.height,self.width,3)
return BGR
#for legacy reasons.
property img:
def __get__(self):
return self.bgr
cdef class AvFrame(Frame):
cdef object _av_frame
cdef object _av_packet
cdef public double timestamp
cdef dict _av_to_ss_map
cdef bint owns_uvc_frame
def __cinit__(self):
self._av_frame = None
self._av_packet = None
self._av_to_ss_map = {'yuv420p' : (4,2,0),
'yuv422p' : (4,2,2)}
def __init__(self, unsigned long uvc_frame, av_frame):
super().__init__('H264','YUV')
self._av_frame = av_frame
self._uvc_frame = <uvc.uvc_frame *> uvc_frame
self.owns_uvc_frame = True
def __dealloc__(self):
if self.owns_uvc_frame:
uvc.uvc_free_frame(self._uvc_frame)
cdef attach_avframe(self, av_frame):
self._av_frame = av_frame
property subsampling:
def __get__(self):
return self._av_to_ss_map[self._av_frame.format.name]
#def __get__(self):
# if self._av_frame.format.name == 'yuv420p':
# return YUV_420
# elif self._av_frame.format.name == 'yuv422p':
# return YUV_422
property width:
def __get__(self):
return self._av_frame.width
property height:
def __get__(self):
return self._av_frame.height
property index:
def __get__(self):
return self._av_frame.index
property yuv_buffer:
def __get__(self):
if self._av_frame.format.name == 'yuv420p':
#print("Y plane w {} h {} buf_size {}".format(self._av_frame.planes[0].width,
# self._av_frame.planes[0].height,
# self._av_frame.planes[0].buffer_size))
#y = np.frombuffer(self._av_frame.planes[0],)
#u = np.frombuffer(self._av_frame.planes[1])
#v = np.frombuffer(self._av_frame.planes[2])
#print("y shape = {} u shape = {} v shape = {} ".format(y.shape, u.shape, v.shape))
#buf = np.concatenate((y,u,v))
#print("buf shape = {}".format(buf.shape))
return np.concatenate((np.frombuffer(self._av_frame.planes[0],dtype=np.uint8),
np.frombuffer(self._av_frame.planes[1], dtype=np.uint8),
np.frombuffer(self._av_frame.planes[2], dtype=np.uint8)))
cdef class TjFrame(Frame):
'''
The Frame Object holds image data and image metadata.
The Frame object is returned from Capture.get_frame()
It will hold the data in the transport format the Capture is configured to grab.
Usually this is mjpeg or yuyv
Other formats can be requested and will be converted/decoded on the fly.
Frame will use caching to avoid redunant work.
Usually RGB8,YUYV or GRAY are requested formats.
WARNING:
When capture.get_frame() is called again previos instances of Frame will point to invalid memory.
Specifically all image data in the capture transport format.
Previously converted formats are still valid.
'''
cdef turbojpeg.tjhandle tj_context
cdef unsigned char[:] _bgr_buffer, _gray_buffer,_yuv_buffer #we use numpy for memory management.
cdef bint _yuv_converted
cdef public double timestamp
cdef public yuv_subsampling
cdef bint owns_uvc_frame
cdef dict _tj_to_ss_map
def __cinit__(self):
#print("Tj frame __cinit__")
self._yuv_converted = False
self._bgr_converted = False
self.tj_context = NULL
self._tj_to_ss_map = { turbojpeg.TJSAMP_422: (4,2,2),
turbojpeg.TJSAMP_420: (4,2,0),
turbojpeg.TJSAMP_444: (4,4,4)
}
def __init__(self, unsigned long tj_context):
super().__init__('MJPEG','YUV')
#print("Tj frame __init__")
cdef turbojpeg.tjhandle tj_handle = <turbojpeg.tjhandle > tj_context
self.tj_context = tj_handle
#print("End of __init__")
cdef attach_uvcframe(self,uvc.uvc_frame *uvc_frame,copy=True):
if copy:
self._uvc_frame = uvc.uvc_allocate_frame(uvc_frame.data_bytes)
uvc.uvc_duplicate_frame(uvc_frame,self._uvc_frame)
self.owns_uvc_frame = True
else:
self._uvc_frame = uvc_frame
self.owns_uvc_frame = False
def __dealloc__(self):
if self.owns_uvc_frame:
uvc.uvc_free_frame(self._uvc_frame)
property subsampling:
def __get__(self):
return self._tj_to_ss_map[self.yuv_subsampling]
property width:
def __get__(self):
return self._uvc_frame.width
property height:
def __get__(self):
return self._uvc_frame.height
property index:
def __get__(self):
return self._uvc_frame.sequence
property jpeg_buffer:
def __get__(self):
cdef np.uint8_t[::1] view = <np.uint8_t[:self._uvc_frame.data_bytes]>self._uvc_frame.data
return view
property raw_buffer:
def __get__(self):
return self.jpeg_buffer
property yuv_buffer:
def __get__(self):
if self._yuv_converted is False:
self.jpeg2yuv()
cdef np.uint8_t[::1] view = <np.uint8_t[:self._yuv_buffer.shape[0]]>&self._yuv_buffer[0]
return view
property yuv420:
def __get__(self):
'''
planar YUV420 returned in 3 numpy arrays:
420 subsampling:
Y(height,width) U(height/2,width/2), V(height/2,width/2)
'''
if self._yuv_converted is False:
self.jpeg2yuv()
return super().yuv420
#cdef np.ndarray[np.uint8_t, ndim=2] Y,U,V
#y_plane_len = self.width*self.height
#Y = np.asarray(self._yuv_buffer[:y_plane_len]).reshape(self.height,self.width)
#if self.yuv_subsampling == turbojpeg.TJSAMP_422:
# uv_plane_len = y_plane_len//2
# offset = y_plane_len
# U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width/2)
# offset += uv_plane_len
# V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width/2)
# #hack solution to go from YUV422 to YUV420
# U = U[::2,:]
# V = V[::2,:]
#elif self.yuv_subsampling == turbojpeg.TJSAMP_420:
# uv_plane_len = y_plane_len//4
# offset = y_plane_len
# U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height/2,self.width/2)
# offset += uv_plane_len
# V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height/2,self.width/2)
#elif self.yuv_subsampling == turbojpeg.TJSAMP_444:
# uv_plane_len = y_plane_len
# offset = y_plane_len
# U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width)
# offset += uv_plane_len
# V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width)
# #hack solution to go from YUV444 to YUV420
# U = U[::2,::2]
# V = V[::2,::2]
#return Y,U,V
property yuv422:
def __get__(self):
'''
planar YUV420 returned in 3 numpy arrays:
422 subsampling:
Y(height,width) U(height,width/2), V(height,width/2)
'''
if self._yuv_converted is False:
self.jpeg2yuv()
cdef np.ndarray[np.uint8_t, ndim=2] Y,U,V
y_plane_len = self.width*self.height
Y = np.asarray(self._yuv_buffer[:y_plane_len]).reshape(self.height,self.width)
if self.yuv_subsampling == turbojpeg.TJSAMP_422:
uv_plane_len = y_plane_len//2
offset = y_plane_len
U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width//2)
offset += uv_plane_len
V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width//2)
elif self.yuv_subsampling == turbojpeg.TJSAMP_420:
raise Exception("can not convert from YUV420 to YUV422")
elif self.yuv_subsampling == turbojpeg.TJSAMP_444:
uv_plane_len = y_plane_len
offset = y_plane_len
U = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width)
offset += uv_plane_len
V = np.asarray(self._yuv_buffer[offset:offset+uv_plane_len]).reshape(self.height,self.width)
#hack solution to go from YUV444 to YUV420
U = U[:,::2]
V = V[:,::2]
return Y,U,V
property gray:
def __get__(self):
# return gray aka luminace plane of YUV image.
if self._yuv_converted is False:
self.jpeg2yuv()
cdef np.ndarray[np.uint8_t, ndim=2] Y
Y = np.asarray(self._yuv_buffer[:self.width*self.height]).reshape(self.height,self.width)
return Y
property bgr:
def __get__(self):
if self._bgr_converted is False:
if self._yuv_converted is False:
self.jpeg2yuv()
self.yuv2bgr()
cdef np.ndarray[np.uint8_t, ndim=3] BGR
BGR = np.asarray(self._bgr_buffer).reshape(self.height,self.width,3)
return BGR
#for legacy reasons.
property img:
def __get__(self):
return self.bgr
cdef yuv2bgr(self):
#2.75 ms at 1080p
cdef int channels = 3
cdef int result
self._bgr_buffer = np.empty(self.width*self.height*channels, dtype=np.uint8)
result = turbojpeg.tjDecodeYUV(self.tj_context, &self._yuv_buffer[0], 4, self.yuv_subsampling,
&self._bgr_buffer[0], self.width, 0, self.height, turbojpeg.TJPF_BGR, 0)
if result == -1:
logger.error('Turbojpeg yuv2bgr: %s'%turbojpeg.tjGetErrorStr() )
self._bgr_converted = True
cdef jpeg2yuv(self):
# 7.55 ms on 1080p
cdef int channels = 1
cdef int jpegSubsamp, j_width,j_height
cdef int result
cdef long unsigned int buf_size
result = turbojpeg.tjDecompressHeader2(self.tj_context, <unsigned char *>self._uvc_frame.data,
self._uvc_frame.data_bytes, &j_width, &j_height, &jpegSubsamp)
if result == -1:
logger.error('Turbojpeg could not read jpeg header: %s'%turbojpeg.tjGetErrorStr() )
# hacky creation of dummy data, this will break if capture does work with different subsampling:
j_width, j_height, jpegSubsamp = self.width, self.height, turbojpeg.TJSAMP_422
buf_size = turbojpeg.tjBufSizeYUV(j_width, j_height, jpegSubsamp)
self._yuv_buffer = np.empty(buf_size, dtype=np.uint8)
if result !=-1:
result = turbojpeg.tjDecompressToYUV(self.tj_context,
<unsigned char *>self._uvc_frame.data,
self._uvc_frame.data_bytes,
&self._yuv_buffer[0],
0)
if result == -1:
logger.warning('Turbojpeg jpeg2yuv: %s'%turbojpeg.tjGetErrorStr() )
self.yuv_subsampling = jpegSubsamp
self._yuv_converted = True
def clear_caches(self):
self._bgr_converted = False
self._yuv_converted = False
cdef class Device_List(list):
cdef uvc.uvc_context_t * ctx
def __cinit__(self):
self.ctx = NULL
cdef int ret = uvc.uvc_init(&self.ctx,NULL)
if ret !=uvc.UVC_SUCCESS:
raise InitError("Could not initialize uvc context.")
def __init__(self):
self.update()
cpdef update(self):
cdef uvc.uvc_device_t ** dev_list
cdef uvc.uvc_device_t * dev
cdef uvc.uvc_device_descriptor_t *desc
if self.ctx == NULL:
raise EnvironmentError("Device list uvc context is NULL.")
ret = uvc.uvc_get_device_list(self.ctx,&dev_list)
if ret !=uvc.UVC_SUCCESS:
logger.error("could not get devices list.")
return
devices = []
cdef int idx = 0
while True:
dev = dev_list[idx]
if dev == NULL:
break
if (uvc.uvc_get_device_descriptor(dev, &desc) == uvc.UVC_SUCCESS):
product = desc.product or "unknown"
manufacturer = desc.manufacturer or "unknown"
serialNumber = desc.serialNumber or "unknown"
idProduct,idVendor = desc.idProduct,desc.idVendor
device_address = uvc.uvc_get_device_address(dev)
bus_number = uvc.uvc_get_bus_number(dev)
devices.append({'name':_to_str(product),
'manufacturer':_to_str(manufacturer),
'serialNumber':_to_str(serialNumber),
'idProduct':idProduct,
'idVendor':idVendor,
'device_address':device_address,
'bus_number':bus_number,
'uid':'%s:%s'%(bus_number,device_address)})
uvc.uvc_free_device_descriptor(desc)
idx +=1
uvc.uvc_free_device_list(dev_list, 1)
self[:] = devices
cpdef cleanup(self):
if self.ctx !=NULL:
uvc.uvc_exit(self.ctx)
self.ctx = NULL
def __dealloc__(self):
self.cleanup()
def device_list():
cdef uvc.uvc_context_t * ctx
cdef int ret = uvc.uvc_init(&ctx,NULL)
if ret !=uvc.UVC_SUCCESS:
logger.error("could not initialize")
return
cdef uvc.uvc_device_t ** dev_list
cdef uvc.uvc_device_t * dev
cdef uvc.uvc_device_descriptor_t *desc
ret = uvc.uvc_get_device_list(ctx,&dev_list)
if ret !=uvc.UVC_SUCCESS:
logger.error("could not get devices list.")
return
devices = []
cdef int idx = 0
while True:
dev = dev_list[idx]
if dev == NULL:
break
if (uvc.uvc_get_device_descriptor(dev, &desc) == uvc.UVC_SUCCESS):
product = desc.product or "unknown"
manufacturer = desc.manufacturer or "unknown"
serialNumber = desc.serialNumber or "unknown"
idProduct,idVendor = desc.idProduct,desc.idVendor
device_address = uvc.uvc_get_device_address(dev)
bus_number = uvc.uvc_get_bus_number(dev)
devices.append({'name':_to_str(product),
'manufacturer':_to_str(manufacturer),
'serialNumber':_to_str(serialNumber),
'idProduct':idProduct,
'idVendor':idVendor,
'device_address':device_address,
'bus_number':bus_number,
'uid':'%s:%s'%(bus_number,device_address)})
uvc.uvc_free_device_descriptor(desc)
idx +=1
uvc.uvc_free_device_list(dev_list, 1)
uvc.uvc_exit(ctx)
return devices
include 'controls.pxi'
cdef class Capture:
"""
Video Capture class.
A Class giving access to a capture UVC devices.
The intent is to grab mjpeg frames and give access to the buffer using the Frame class.
All controls are exposed and can be enumerated using the controls list.
"""
cdef turbojpeg.tjhandle tj_context
cdef uvc.uvc_context_t *ctx
cdef uvc.uvc_device_t *dev
cdef uvc.uvc_device_handle_t *devh
cdef uvc.uvc_stream_ctrl_t ctrl
cdef bint _stream_on,_configured
cdef uvc.uvc_stream_handle_t *strmh
cdef float _bandwidth_factor
cdef tuple _active_mode
cdef list _available_modes
cdef dict _info
cdef public list controls
cdef object codec
cdef object codec_context
cdef list decoded_frames
cdef uvc.uvc_frame * _uvc_frame
cdef int num_frames
cdef double avg_time
def __cinit__(self,dev_uid):
self.dev = NULL
self.ctx = NULL
self.devh = NULL
self._stream_on = 0
self._configured = 0
self.strmh = NULL
self._available_modes = []
self._active_mode = None,None,None,None
self._info = {}
self.controls = []
self._bandwidth_factor = 2.0
self.codec = Codec('h264', 'r')
self.codec_context = self.codec.create()
self.decoded_frames = []
self.num_frames = 0
self.avg_time = 0.
def __init__(self,dev_uid):
#setup for jpeg converter
self.tj_context = turbojpeg.tjInitDecompress()
if uvc.uvc_init(&self.ctx, NULL) != uvc.UVC_SUCCESS:
raise InitError('Could not init libuvc')
self._init_device(dev_uid)
self._enumerate_formats()
self._enumerate_controls()
cdef _init_device(self,dev_uid):
##first we find the appropriate dev handle
cdef uvc.uvc_device_t ** dev_list
cdef uvc.uvc_device_descriptor_t *desc
cdef int idx = 0
cdef int error
if uvc.uvc_get_device_list(self.ctx,&dev_list) !=uvc.UVC_SUCCESS:
uvc.uvc_exit(self.ctx)
raise InitError("could not get devices list.")
while True:
dev = dev_list[idx]
if dev == NULL:
break
device_address = uvc.uvc_get_device_address(dev)
bus_number = uvc.uvc_get_bus_number(dev)
if dev_uid == '%s:%s'%(bus_number,device_address):
logger.debug("Found device that mached uid:'%s'"%dev_uid)
uvc.uvc_ref_device(dev)
if (uvc.uvc_get_device_descriptor(dev, &desc) == uvc.UVC_SUCCESS):
product = desc.product or "unknown"
manufacturer = desc.manufacturer or "unknown"
serialNumber = desc.serialNumber or "unknown"
idProduct,idVendor = desc.idProduct,desc.idVendor
device_address = uvc.uvc_get_device_address(dev)
bus_number = uvc.uvc_get_bus_number(dev)
self._info = {'name':_to_str(product),
'manufacturer':_to_str(manufacturer),
'serialNumber':_to_str(serialNumber),
'idProduct':idProduct,
'idVendor':idVendor,
'device_address':device_address,
'bus_number':bus_number,
'uid':'%s:%s'%(bus_number,device_address)}
uvc.uvc_free_device_descriptor(desc)
break
idx +=1
uvc.uvc_free_device_list(dev_list, 1)
if dev == NULL:
raise DeviceNotFoundError("Device with uid: '%s' not found"%dev_uid)
#once found we open the device
self.dev = dev
error = uvc.uvc_open(self.dev,&self.devh)
if error != uvc.UVC_SUCCESS:
raise OpenError("could not open device. Error:%s"%uvc_error_codes[error])
logger.debug("Device '%s' opended."%dev_uid)
cdef _de_init_device(self):
uvc.uvc_close(self.devh)
self.devh = NULL
uvc.uvc_unref_device(self.dev)
self.dev = NULL
logger.debug('UVC device closed.')
cdef _restart(self):
self._stop()
self._re_init_device()
self._start()
cdef _configure_stream(self,mode=(640,480,30, 'MJPEG')):
cdef int status
if self._stream_on:
self._stop()
status = uvc.uvc_get_stream_ctrl_format_size( self.devh, &self.ctrl,
_str_to_fmt(mode[3]), # uvc.UVC_FRAME_FORMAT_H264,
mode[0],mode[1],mode[2] )
if status != uvc.UVC_SUCCESS:
raise InitError("Can't get stream control: Error:'%s'."%uvc_error_codes[status])
self._configured = 1
self._active_mode = mode
self.num_frames = 0
cdef _start(self):
cdef int status
if not self._configured:
self._configure_stream()
status = uvc.uvc_stream_open_ctrl(self.devh, &self.strmh, &self.ctrl)
if status != uvc.UVC_SUCCESS:
raise InitError("Can't open stream control: Error:'%s'."%uvc_error_codes[status])
status = uvc.uvc_stream_start(self.strmh, NULL, NULL,self._bandwidth_factor,0)
if status != uvc.UVC_SUCCESS:
raise InitError("Can't start isochronous stream: Error:'%s'."%uvc_error_codes[status])
self._stream_on = 1
logger.debug("Stream start.")
def stop_stream(self):
self._stop()
cdef _stop(self):
cdef int status = 0
status = uvc.uvc_stream_stop(self.strmh)
if status != uvc.UVC_SUCCESS:
#raise Exception("Can't stop stream: Error:'%s'."%uvc_error_codes[status])
logger.error("Can't stop stream: Error:'%s'. Will ignore this and try to continue."%uvc_error_codes[status])
else:
logger.debug("Stream stopped")
uvc.uvc_stream_close(self.strmh)
logger.debug("Stream closed")
self._stream_on = 0
logger.debug("Stream stop.")
def get_frame_robust(self):
cdef int a,attempts = 3
for a in range(attempts):
try:
frame = self.get_frame()
except StreamError as e:
if a:
logger.info('Could not get Frame: "%s". Attempt:%s/%s '%(e.message,a+1,attempts))
else:
logger.debug('Could not get Frame of first try: "%s". Attempt:%s/%s '%(e.message,a+1,attempts))
else:
return frame
raise StreamError("Could not grab frame after 3 attempts. Giving up.")
def get_frame(self,timeout=0):
#test
#timeout += 0.3
#timeout = 3.
cdef int status, j_width,j_height,jpegSubsamp,header_ok
cdef int timeout_usec = int(timeout*1e6) #sec to usec
if not self._stream_on:
self._start()
cdef uvc.uvc_frame *uvc_frame = NULL
packet = Packet()
cdef list dec_frames
#cdef Frame out_frame = Frame()
#if len(self.decoded_frames) > 0:
# uvc_frame
# cdef Frame out_frame = Frame()
# out_frame.tj_context = None
# out_frame.attach_uvcframe(uvc_frame = uvc_frame,copy=True)
# out_frame.timestamp = uvc_frame.capture_time.tv_sec + <double>uvc_frame.capture_time.tv_usec * 1e-6
cdef Frame out_frame_av
cdef Frame out_frame
if self._active_mode[3] == 'H264':
timeout = 0
while True:
#when this is called we will overwrite the last jpeg buffer! This can be dangerous!
with nogil:
status = uvc.uvc_stream_get_frame(self.strmh,&uvc_frame,timeout_usec)
#print("Got frame {0:x}".format(<unsigned long>uvc_frame))
if status !=uvc.UVC_SUCCESS:
raise StreamError(uvc_error_codes[status])
if uvc_frame is NULL:
raise StreamError("Frame pointer is NULL")
#print("Got uvc frame!")
self._uvc_frame = uvc.uvc_allocate_frame(uvc_frame.data_bytes)
uvc.uvc_duplicate_frame(uvc_frame,self._uvc_frame)
packet.set_payload_ptr(<unsigned long>self._uvc_frame.data, self._uvc_frame.data_bytes)
dec_start = monotonic()
dec_frames = self.codec_context.decode(packet)
self.num_frames += 1
dec_time = monotonic() - dec_start
self.avg_time = (self.avg_time * (self.num_frames - 1) + dec_time) / self.num_frames
#print("Av Decode time {}".format(self.avg_time))
if len(dec_frames) > 0:
av_frame = dec_frames.pop()
#print("Av frame is {}".format(av_frame))
out_frame_av = AvFrame(<unsigned long> self._uvc_frame, av_frame)
#out_frame_av.attach_avframe(av_frame)
out_frame_av.timestamp = uvc_frame.capture_time.tv_sec + <double>uvc_frame.capture_time.tv_usec * 1e-6
#print("H264 frame!")
#uvc.uvc_free_frame(self._uvc_frame)
return out_frame_av
else:
uvc.uvc_free_frame(self._uvc_frame)
else:
with nogil:
status = uvc.uvc_stream_get_frame(self.strmh,&uvc_frame,timeout_usec)
if status != uvc.UVC_SUCCESS:
raise StreamError(uvc_error_codes[status])
if uvc_frame is NULL:
raise StreamError("Frame pointer is NULL")
#print("Got mjpeg frame!")
out_frame = TjFrame(<unsigned long>self.tj_context)
#print("Tj frame created")
#out_frame.tj_context = self.tj_context
out_frame.attach_uvcframe(uvc_frame = uvc_frame,copy=True)
header_ok = turbojpeg.tjDecompressHeader2(self.tj_context, <unsigned char *>uvc_frame.data, uvc_frame.data_bytes, &j_width, &j_height, &jpegSubsamp)
#print("header_ok = {}".format(header_ok))
if not (header_ok >=0 and uvc_frame.width == j_width and uvc_frame.height == j_height):
raise StreamError("JPEG header corrupt.")
out_frame.timestamp = uvc_frame.capture_time.tv_sec + <double>uvc_frame.capture_time.tv_usec * 1e-6
return out_frame
##check jpeg header
#header_ok = turbojpeg.tjDecompressHeader2(self.tj_context, <unsigned char *>uvc_frame.data, uvc_frame.data_bytes, &j_width, &j_height, &jpegSubsamp)
#print("header_ok = {}".format(header_ok))
#if not (header_ok >=0 and uvc_frame.width == j_width and uvc_frame.height == j_height):
# raise StreamError("JPEG header corrupt.")
#cdef Frame out_frame = Frame()
#out_frame.tj_context = self.tj_context
#out_frame.attach_uvcframe(uvc_frame = uvc_frame,copy=True)
#out_frame.timestamp = uvc_frame.capture_time.tv_sec + <double>uvc_frame.capture_time.tv_usec * 1e-6
#cdef AvFrame out_frame = AvFrame()
#out_frame.attach_avframe(av_frame)
cdef _enumerate_controls(self):
cdef uvc.uvc_input_terminal_t *input_terminal = uvc.uvc_get_input_terminals(self.devh)
cdef uvc.uvc_output_terminal_t *output_terminal = uvc.uvc_get_output_terminals(self.devh)
cdef uvc.uvc_processing_unit_t *processing_unit = uvc.uvc_get_processing_units(self.devh)
cdef uvc.uvc_extension_unit_t *extension_unit = uvc.uvc_get_extension_units(self.devh)
cdef int x = 0
avaible_controls_per_unit = {}
id_per_unit = {}
extension_units = {}
while extension_unit !=NULL:
guidExtensionCode = uint_array_to_GuidCode(extension_unit.guidExtensionCode)
id_per_unit[guidExtensionCode] = extension_unit.bUnitID
avaible_controls_per_unit[guidExtensionCode] = extension_unit.bmControls
extension_unit = extension_unit.next
while input_terminal !=NULL:
avaible_controls_per_unit['input_terminal'] = input_terminal.bmControls
id_per_unit['input_terminal'] = input_terminal.bTerminalID
input_terminal = input_terminal.next
while processing_unit !=NULL:
avaible_controls_per_unit['processing_unit'] = processing_unit.bmControls
id_per_unit['processing_unit'] = processing_unit.bUnitID
processing_unit = processing_unit.next
cdef Control control
for std_ctl in standard_ctrl_units:
if std_ctl['bit_mask'] & avaible_controls_per_unit[std_ctl['unit']]:
logger.debug('Adding "%s" control.'%std_ctl['display_name'])
std_ctl['unit_id'] = id_per_unit[std_ctl['unit']]
try:
control= Control(cap = self,**std_ctl)
except Exception as e:
logger.error("Could not init '%s'! Error: %s" %(std_ctl['display_name'],e))
else:
self.controls.append(control)
#uvc.PyEval_InitThreads()
#uvc.uvc_set_status_callback(self.devh, on_status_update,<void*>self)
cdef _enumerate_formats(self):
cdef uvc.uvc_streaming_interface_t *streaming_if = uvc.uvc_get_streaming_ifs(self.devh)
cdef uvc.uvc_format_desc_t *format_desc
cdef uvc.uvc_frame_desc *frame_desc
cdef int i
self._available_modes = []
while streaming_if is not NULL:
format_desc = streaming_if.format_descs