Skip to content

Instantly share code, notes, and snippets.

@Anniepoo
Created August 18, 2018 05:06
Show Gist options
  • Save Anniepoo/afd50979561fb6b95de7a0dac0728c7f to your computer and use it in GitHub Desktop.
Save Anniepoo/afd50979561fb6b95de7a0dac0728c7f to your computer and use it in GitHub Desktop.
#!/usr/bin/python
'''
cam_server.py
'''
import SimpleHTTPServer
import SocketServer
from urlparse import urlparse
from urlparse import parse_qs
from threading import Thread, Lock
import time
import io
import picamera
camera = picamera.PiCamera()
camera.start_preview()
time.sleep(2)
def get_image(output, search):
camera.capture(output, 'bgr')
mutex = Lock()
PORT = 8020
# expecting a GET request on http://192.168.254.65:8020?<search string>
#
# hsize - horizontal size default 1280
# vsize - vertical size default 720
#
#
# valid sizes
# 3280 x 2464 (4:3)
# 64 x 48 (4:3)
# 640 x 480 (4:3)
# 1920 x 1080 (16:9)
# 1280 x 720 (16:9)
#
# This says https://www.raspberrypi.org/blog/new-camera-mode-released/
# Stills outputs 4:3 (like old school TV), video 16:9 (wide screen).
#
# camera version 1 table of resolutions
# https://picamera.readthedocs.io/en/release-1.10/fov.html
#
class MyHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
mutex.acquire()
try:
self.send_response(200)
self.send_header('Content-Type', 'image/bgr')
self.end_headers()
url = urlparse(self.path)
print(url.path)
if((url.path == '') or (url.path=='/')):
get_image(self.wfile, parse_qs(url.query))
else:
self.wfile.write(str(0))
finally:
mutex.release()
Handler = MyHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), MyHTTPRequestHandler)
httpd.serve_forever()
#!/usr/bin/python
'''
cv_core.py
'''
'''import SimpleHTTPServer
import SocketServer
from urlparse import urlparse
from urlparse import parse_qs
from threading import Thread, Lock
import time
'''
import io
import cv2
import numpy as np
from urllib.request import urlopen
# 2.7 version
# https://stackoverflow.com/questions/15138614/how-can-i-read-the-contents-of-an-url-with-python#15138702
link = "http://192.168.254.65:8020/"
f = urlopen(link)
#image = np.array(f.read())
data = np.fromstring(f.read(99999999), dtype=np.uint8)
print(len(data))
# "Decode" the image from the array, preserving colour
#image = cv2.imdecode(data, 1)
#print(len(image))
cv2.imwrite('hopefullyimage.jpg', data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment