python-2.7


Sending images using sockets in python


My intention for this program is that it sends an image from the Raspberry pi camera to a client which then can analyse the image with a neural network (uses python).
My problem is that I currently get the following error and am unable to get any data on the client side:
line 16, in <module>
d = connection.read()
io.UnsupportedOperation: read
Idea's for a more robust solution are welcome to as I am a novice with network programming.
client
import socket
import io
from PIL import Image
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('XXX.XXX.XXX.XXX', XXXX))
# Make a file-like object out of the connection
connection = client_socket.makefile('wb')
while 1:
message = []
while True:
d = connection.read(1024*1024)
if not d: break
else: message.append(d)
data = ''.join(message)
im = io.StringIO(data)
#print(data)
image = Image.open(io.BytesIO(im))
#image.show()
connection.close()
client_socket.close()
server
import io
import time
import socket
import picamera
from PIL import Image
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
PORT = XXXX
for x in range(0, 5):
try:
server_socket.bind(('0.0.0.0', PORT + x))
print('Using PORT: ', PORT + x)
except socket.error as exc:
pass
else:
break
server_socket.listen(0)
# Accept a single connection and make a file-like object out of it
connection = server_socket.accept()[0].makefile('wb')
# Create the in-memory stream
stream = io.BytesIO()
with picamera.PiCamera() as camera:
#camera.start_preview()
camera.resolution = (600, 600)
time.sleep(1)
camera.capture(stream, format='jpeg')
connection.write(stream.read())
connection.flush()
stream.seek(0)
stream.truncate()
server_socket.close()
You open the socket with mode 'wb' but then you want to read from it? I think you need client_socket.make file('rb')

Related Links

Tkinter grid problems
ValueError: Invalid header value 'H2O Python client/2.7.9 (default, Apr 2 2015, 15:33:21) \n[GCC 4.9.2]'
Twitter search with urllib2 failing
is mayavi ready for enaml or stick with traits for gui design?
Check for values return from EnumValue
python socket programming with Tkinter GUI
TypeError: unsupported operand type(s) for 'Instance' and 'Int'
Generate a dictionnary in a list
Why this python script does not work?
Selenium Webdriver Python page object MainPageLocators class why does it use an asterisk infront of the class name
Is it possible to stop Tkinter window freezing during update?
How can I require an api-token field on requests?
pip error while installing mayavi
import matplotlib._png as _png Import: Error: DLL load failed: The specified module could not be found
Retrieving data from OPC-Server
Python: ascii codec can't encode en-dash

Categories

HOME
webview
generics
tomcat
apple-push-notifications
barcode-scanner
couchdb
haproxy
activemq
dry
odata
nsis
web2py
elisp
video-streaming
tfs2010
jpa-criteria
yahoo
vimeo
cname
webstore
mahout-recommender
l20n
http-authentication
azure-machine-learning
web-config-transform
jackrabbit-oak
miniprofiler
cube
region
docx4j
substance
guice
locks
es6-modules
toad
fotorama
seafile-server
cucumber-junit
sql-execution-plan
variable-variables
aurelia-http-client
rdp
best-buy-api
jacoco-maven-plugin
hypothesis-test
word-cloud
fiware-wirecloud
yoast
liteide
section508
return-type
supertest
visual-format-language
gtk#
portability
mechanicalturk
cognos-tm1
yui3
java-2d
dcount
jpda
data-management
nsdata
qtcpsocket
timestamping
textblock
cortana
boost-proto
textscan
quercus
mgwt
rfc5545
html-escape-characters
maven-shade-plugin
pass-by-value
lifetime
encryption-symmetric
vs-unit-testing-framework
dc
inputbox
treeline
maven-central
xulrunner
profiles
compiler-flags
frontbase
unicoins
ckeditor.net
ip-geolocation
story
zend-pdf
clrprofiler
clearinterval
workflow-manager-1.x
first-class
server-name
algol68
vows
getopts
office-automation
digiflow
raw-data
processors
autobench
acceleration
levels
service-factory

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App