create time:2019年7月8日
??我在網上找了好多,現在基本實現了傳輸視頻的功能,其思路是:首先得先了解如何使用openCV采集攝像頭數據,
其次得了解python socket網絡編程。因為socket不能直接傳輸openCV采集的數據,所以還需要轉碼操作。在該版本我實現最基
本的功能,客戶端采集,服務端接收(也可以倒過來,看個人需求),使用類實現。
服務端
#!usr/bin/python
# coding=utf-8
import
socket
import
cv2
import
numpy
class
VideoServer
:
def
__init__
(
self
)
:
try
:
self
.
s_sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
s_sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
self
.
s_sock
.
bind
(
(
'10.0.0.30'
,
9999
)
)
self
.
s_sock
.
listen
(
5
)
except
Exception
,
err
:
print
err
exit
(
0
)
def
recv_all
(
self
,
sock
,
count
)
:
buf
=
''
while
count
:
newbuf
=
sock
.
recv
(
count
)
if
not
newbuf
:
return
None
buf
+=
newbuf
count
-=
len
(
newbuf
)
return
buf
def
recvImgAndShow
(
self
)
:
c_sock
,
c_addr
=
self
.
s_sock
.
accept
(
)
while
True
:
tempdata_len
=
self
.
recv_all
(
c_sock
,
16
)
if
len
(
tempdata_len
)
==
16
:
stringData
=
self
.
recv_all
(
c_sock
,
int
(
tempdata_len
)
)
data
=
numpy
.
fromstring
(
stringData
,
dtype
=
'uint8'
)
tmp
=
cv2
.
imdecode
(
data
,
1
)
# 解碼處理,返回mat圖片
img
=
cv2
.
resize
(
tmp
,
(
640
,
480
)
)
cv2
.
imshow
(
'SERVER'
,
img
)
if
cv2
.
waitKey
(
1
)
==
27
:
break
self
.
s_sock
.
close
(
)
cv2
.
destroyAllWindows
(
)
if
__name__
==
'__main__'
:
videoS
=
VideoServer
(
)
videoS
.
recvImgAndShow
(
)
客戶端
#!usr/bin/python
# coding=utf-8
import
socket
import
cv2
import
numpy
class
CaptureCam
:
def
__init__
(
self
)
:
# 連接服務器(初始化)
self
.
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
sock
.
connect
(
(
'10.0.0.30'
,
9999
)
)
# 采集視頻(參數)
self
.
resolution
=
(
640
,
480
)
# 分辨率
self
.
img_fps
=
95
# each second send pictures
self
.
img
=
''
self
.
img_data
=
''
def
sendfun
(
self
)
:
camera
=
cv2
.
VideoCapture
(
0
)
img_param
=
[
int
(
cv2
.
IMWRITE_JPEG_QUALITY
)
,
self
.
img_fps
]
while
True
:
_
,
self
.
img
=
camera
.
read
(
)
self
.
img
=
cv2
.
resize
(
self
.
img
,
self
.
resolution
)
_
,
img_encode
=
cv2
.
imencode
(
'.jpg'
,
self
.
img
,
img_param
)
img_code
=
numpy
.
array
(
img_encode
)
self
.
img_data
=
img_code
.
tostring
(
)
try
:
self
.
sock
.
send
(
str
(
len
(
self
.
img_data
)
)
.
ljust
(
16
)
)
self
.
sock
.
send
(
self
.
img_data
)
except
Exception
,
err
:
print
err
camera
.
release
(
)
self
.
sock
.
close
(
)
exit
(
0
)
if
__name__
==
'__main__'
:
cam
=
CaptureCam
(
)
cam
.
sendfun
(
)
點
- python opencv imencode跟imdecode函數用法
經典用法
_
,
img_encode
=
cv2
.
imencode
(
'.jpg'
,
img
,
img_param
)
- img 是一張圖片文件(在openCV中就是一個numpy的多維矩陣)
- img_param 是壓縮參數
img_param
=
[
int
(
cv2
.
IMWRITE_JPEG_QUALITY
)
,
img_fps
]
- cv2.IMWRITE_JPEG_QUALITY 是openCV中的一個參數(類型是long這里需要做int轉換)
- 對于jpg來說,imf_fps這個地方的數字可以是0到100,默認95。數字越大圖片質量越好
參考:
https://blog.csdn.net/a19990412/article/details/80940310
https://blog.csdn.net/li_dongxuan/article/details/73135364
https://blog.csdn.net/a19990412/article/details/80930725
https://www.jianshu.com/p/4aed39710676
https://blog.csdn.net/hackjames/article/details/6943426
https://www.cnblogs.com/Leo_wl/p/3335877.html
python socket模塊詳解
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
