2017-09-04 81 views
0

我正在嘗試使用python將來自Raspberry Pi的視頻流傳輸到我的機器。因此,我需要將套接字連接到服務器。 (流視頻直接在192.168.0.6:8081從RPI執行這些之前)OSError:[WinError 10049]我該如何處理這個錯誤?

在我的電腦上我執行此代碼:

import numpy as np 
import cv2 
import socket 


class VideoStreamingTest(object): 

    def __init__(self): 
     self.server_socket = socket.socket() 
     self.server_socket.bind(('192.168.0.6', 8081)) 
     self.server_socket.listen(0) 
     self.connection, self.client_address = self.server_socket.accept() 
     self.connection = self.connection.makefile('rb') 
     self.streaming() 

def streaming(self): 
    try: 
     print ("Connection from: ", self.client_address) 
     print ("Streaming...") 
     print ("Press 'q' to exit") 

     stream_bytes = ' ' 
     while True: 
      stream_bytes += self.connection.read(1024) 
      first = stream_bytes.find('\xff\xd8') 
      last = stream_bytes.find('\xff\xd9') 
      if first != -1 and last != -1: 
       jpg = stream_bytes[first:last + 2] 
       stream_bytes = stream_bytes[last + 2:] 
       #image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_GRAYSCALE) 
       image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED) 
       cv2.imshow('image', image) 

       if cv2.waitKey(1) & 0xFF == ord('q'): 
        break 
    finally: 
     self.connection.close() 
     self.server_socket.close() 

if __name__ == '__main__': 
    VideoStreamingTest() 

在編譯期間它提出了這樣的錯誤:

Traceback (most recent call last): 

File "C:/Users/tiger/Desktop/take_the_stream_from_pi.py", line 43, in <module> 
    VideoStreamingTest() 

File "C:/Users/tiger/Desktop/take_the_stream_from_pi.py", line 11, in __init__ 
    self.server_socket.bind(('192.168.0.6', 8081)) 

OSError: [WinError 10049] The requested address is not valid in its context 

編輯:這也給

Traceback (most recent call last): 
    File "C:/Users/tiger/Desktop/take_the_stream_from_pi.py", line 47, in 
<module> 
    VideoStreamingTest() 
    File "C:/Users/tiger/Desktop/take_the_stream_from_pi.py", line 17, in 
__init__ 
self.connection = self.socket.create_connection(('192.168.0.6', 8081)) 
AttributeError: 'socket' object has no attribute 'create_connection' 

所以我試圖重新將create_connection與connect_ex,它仍然是給誤差修改.. :(

class VideoStreamingTest(object): 
    def __init__(self): 

    #self.server_socket = socket.socket() 
    #self.server_socket.bind(('192.168.0.6', 8081)) 
    #self.server_socket.listen(0) 

    #self.connection, self.client_address = self.server_socket.accept() 
    #self.connection = self.connection.makefile('rb') 
    #self.streaming() 
    self.socket = socket.socket() 
    self.connection = self.socket.create_connection(('192.168.0.6', 8081)) 
    #self.socket.connect(('192.168.0.6', 8081)) 
    self.streaming() 

def streaming(self): 

    try: 
     #print ("Connection from: ", self.client_address) 
     print ("Streaming...") 
     print ("Press 'q' to exit") 

     stream_bytes = ' ' 
     while True: 
      stream_bytes += self.connection.read(1024) 
      first = stream_bytes.find('\xff\xd8') 
      last = stream_bytes.find('\xff\xd9') 
      if first != -1 and last != -1: 
       jpg = stream_bytes[first:last + 2] 
       stream_bytes = stream_bytes[last + 2:] 
       #image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_GRAYSCALE) 
       image = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.CV_LOAD_IMAGE_UNCHANGED) 
       cv2.imshow('image', image) 

       if cv2.waitKey(1) & 0xFF == ord('q'): 
        break 
    finally: 
     self.connection.close() 
     self.socket.close() 

if __name__ == '__main__': 
    VideoStreamingTest() 
+1

'192.168.0.6'是遠程的IP地址,是否正確? 'socket.bind()'需要分配給本地主機的地址。 – birryree

+0

ya其覆盆子pi的IP地址 –

回答

0

你必須改變在插槽中,設備的IP地址的bind指令的IP地址,其提供的服務器,即地址你自己的電腦。您在此發佈的代碼不會執行您想要執行的操作:它將創建一個新的server,並且不會充當可以連接到服務器的客戶端!

此行

self.server_socket.bind(('192.168.0.6', 8081)) 
self.server_socket.listen(0) 

必須與

self.socket.connect(('192.168.0.6', 8081)) 

編輯被替換:也許是最好使用create_connection功能,因爲它提出了更高的API級別運行。所以,你最終會與此初始化方法:

def __init__(self): 
    self.socket = socket.socket() 
    self.connection, _ = self.socket.create_connection(('192.168.0.6', 8081)) 
    self.streaming() 

streaming方法,那麼你必須刪除此行,也:

print ("Connection from: ", self.client_address) 
+0

我會馬上嘗試,如果它有效,我會告訴你!非常感謝! –

+0

@MohdSalman如果它對你有幫助,請將我的迴應投給我,並將其標記爲你問題的答案。 – FlashTek

+0

錯誤: 回溯(最近通話最後一個): 文件 「C:/Users/tiger/Desktop/take_the_stream_from_pi.py」,第44行,在 VideoStreamingTest()文件「C:/用戶/虎/桌面(''192.168.0.6',8081)) AttributeError:'VideoStreamingTest'對象沒有屬性'socket' –