diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..eb03da0 Binary files /dev/null and b/.DS_Store differ diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/connect.py b/connect.py new file mode 100644 index 0000000..2b34031 --- /dev/null +++ b/connect.py @@ -0,0 +1,23 @@ +import socket + +def new_connection(): + host = '192.168.2.4' + port = 9000 + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((host, port)) + return s; + +def send_command(connection,command,time): + connection.send('' + command + ' ; ' + time) + +def receive_confirmation(connection): + received = connection.recv(20) + return received; + +def send_end_connection(connection): + connection.send('quit') + return + +# example: +# con = new_conntection +# send_command(con,"forward",0.5) diff --git a/connect.pyc b/connect.pyc new file mode 100644 index 0000000..4daa34c Binary files /dev/null and b/connect.pyc differ diff --git a/controllers/.DS_Store b/controllers/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/controllers/.DS_Store differ diff --git a/controllers/__init__.py b/controllers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/controllers/controller.py b/controllers/controller.py index e69de29..3eb5ec9 100644 --- a/controllers/controller.py +++ b/controllers/controller.py @@ -0,0 +1,48 @@ +import os +import sys +from time import sleep +sys.path.insert(0, os.getcwd()) +from models.analyze import check_blob_in_direct_path +from get_images_from_pi import get_image, valid_image +from connect import new_connection, send_command, receive_confirmation,send_end_connection +from start_camera import start_camera +from start_server import start_server +import threading + +#Start-all +threads = [] +#start the server listening on contr pi +server_thread = threading.Thread(target=start_server) +threads.append(server_thread) +server_thread.start() +#Sleep for testing +sleep(2) +#start the camera taking pictures on camera pi +camera_thread = threading.Thread(target=start_camera) +threads.append(camera_thread) +camera_thread.start() +# Sleep to make sure server connects +sleep(5) +#start the server connection on this current server +s = new_connection() + +#Run-All. X times do + #retrieve image. #Blog Detection + #send instruction over server +count = 0 +while (count < 20): + get_image() + if valid_image(os.getcwd() + "/images/gregTest.jpg"): + instruction = check_blob_in_direct_path(os.getcwd() + "/images/gregTest.jpg") + send_command(s, instruction, "0.5") + count += 1 + receive_confirmation(s) + count += 1 + +# #End-all +# #Stop the camera taking pictures on the camera pi -- still needs to be implemented +# #close the server connection +send_end_connection(s) +# #Kill the server listening process +s.close() +exit() diff --git a/distance_to_camera.py b/distance_to_camera.py index ade6403..ebf8549 100644 --- a/distance_to_camera.py +++ b/distance_to_camera.py @@ -2,9 +2,9 @@ import SimpleCV import sys from SimpleCV import Image - + def check_image(image_path): - #Find file by path and import. File currently resides in same directory. + #Find file by path and import. File currently resides in same directory. image = Image(image_path) # grey = image.grayscale() @@ -19,7 +19,7 @@ def check_image(image_path): #check if the blob is in the middle 1/2 of screen and is too high for blob in blobs[:-1]: # print blob.contains() - + if (blob.coordinates()[0] > array_bounds_possible_widths[0] and blob.coordinates()[0] < array_bounds_possible_widths[1]) and (blob.height() > image.height / 5 and blob.coordinates()[1] > image.height / 5 * 2): # print grey.height print blob.coordinates()[1] @@ -28,21 +28,10 @@ def check_image(image_path): instruction = "stop" # Display the blob until you click on the left side of the picture. - display = SimpleCV.Display() - while display.isNotDone(): - image.show() - if display.mouseLeft: - break + #display = SimpleCV.Display() + #while display.isNotDone(): + #image.show() + ##\#if display.mouseLeft: + #break return instruction - -print sys.argv[1] -instruction = check_image(sys.argv[1]) -print instruction - - -# def check_width(image, blob): -# print check_width -# def check_height(image, blob): -# return (blob.height() > image.height / 1.5) and blob - diff --git a/distance_to_camera.pyc b/distance_to_camera.pyc new file mode 100644 index 0000000..12a8d15 Binary files /dev/null and b/distance_to_camera.pyc differ diff --git a/driving_server.py b/driving_server.py new file mode 100644 index 0000000..5ff4c5a --- /dev/null +++ b/driving_server.py @@ -0,0 +1,45 @@ +from socket import * +import run_rc_car as picar + +def createPiReceiver(): + serversocket = socket(AF_INET, SOCK_STREAM) + port = 9000 + new_picar = picar.PiCar() + serversocket.bind(('',port)) + serversocket.listen(5) + print "New Server Created, listening on port " + str(port) + while(1): + (clientsocket, address) = serversocket.accept() + print "accepted connection from " + str(address) + while(1): + transfer = clientsocket.recv(20).split(';') + if not transfer: break + if transfer == ['']: break + print "received: " + transfer + try: + inputParser(transfer[0].strip(),float(transfer[1]),new_picar) + clientsocket.send("true") + except Exception: + clientsocket.send("false") + continue + + +def inputParser(command,number,car): + print 'executing ' + command + ' for ' + str(number) +' seconds' + if command == "forward": + car.go_forward(number) + elif command == "backward": + car.go_backward(number) + elif command == "right": + car.go_forward_right(number) + elif command == "left": + car.go_forward_left(number) + elif command == "backward right": + car.go_backward_right(number) + elif command == "backward left": + car.go_backward_left(number) + else: + car.stop() + + +createPiReceiver() diff --git a/get_images_from_pi.py b/get_images_from_pi.py new file mode 100644 index 0000000..43b6cc1 --- /dev/null +++ b/get_images_from_pi.py @@ -0,0 +1,22 @@ +import os +from PIL import Image + +def get_image_x_times(times): + count = 0 + while count < times: + #replace sleep with the amount of time needed to sleep in between getting pictures. + time.sleep(0.5) + count += 1 + +def get_image(): + os.system('scp pi@192.168.2.5:Desktop/gregTest.jpg gregTest.jpg') + return + #if on a new computer and the pi address isn't connecting right away, need to create a key. Use the following website to do so + #https://www.raspberrypi.org/documentation/remote-access/ssh/passwordless.md + +def valid_image(path): + try: + Image.open(path) + except IOError: + return False + return True diff --git a/get_images_from_pi.pyc b/get_images_from_pi.pyc new file mode 100644 index 0000000..3e3b384 Binary files /dev/null and b/get_images_from_pi.pyc differ diff --git a/greg.jpg b/greg.jpg new file mode 100644 index 0000000..37b49c2 Binary files /dev/null and b/greg.jpg differ diff --git a/gregTest.jpg b/gregTest.jpg new file mode 100644 index 0000000..e69de29 diff --git a/images/five.jpg b/images/five.jpg new file mode 100644 index 0000000..73f96ad Binary files /dev/null and b/images/five.jpg differ diff --git a/images/four.jpg b/images/four.jpg new file mode 100644 index 0000000..f64b20d Binary files /dev/null and b/images/four.jpg differ diff --git a/images/greg.jpg b/images/greg.jpg new file mode 100644 index 0000000..2ca6251 Binary files /dev/null and b/images/greg.jpg differ diff --git a/images/gregTest.jpg b/images/gregTest.jpg new file mode 100644 index 0000000..37a38b9 Binary files /dev/null and b/images/gregTest.jpg differ diff --git a/images/one.jpg b/images/one.jpg new file mode 100644 index 0000000..0b4bf99 Binary files /dev/null and b/images/one.jpg differ diff --git a/images/seven.jpg b/images/seven.jpg new file mode 100644 index 0000000..1c8ce12 Binary files /dev/null and b/images/seven.jpg differ diff --git a/images/six.jpg b/images/six.jpg new file mode 100644 index 0000000..f790546 Binary files /dev/null and b/images/six.jpg differ diff --git a/images/two.jpg b/images/two.jpg new file mode 100644 index 0000000..348a3c0 Binary files /dev/null and b/images/two.jpg differ diff --git a/loop.py b/loop.py new file mode 100644 index 0000000..cba3876 --- /dev/null +++ b/loop.py @@ -0,0 +1,13 @@ +import distance_to_camera as d + + +print "GOod idea" + +instruction = d.check_image('images/two.jpg') + +for x in range(0, 100): + print "We're on time %d" % (x) + instruction = d.check_image('images/two.jpg') + + +exit() diff --git a/models/.DS_Store b/models/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/models/.DS_Store differ diff --git a/models/__init__.py b/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/models/__init__.pyc b/models/__init__.pyc new file mode 100644 index 0000000..8e335bd Binary files /dev/null and b/models/__init__.pyc differ diff --git a/models/analyze.py b/models/analyze.py new file mode 100644 index 0000000..d8e992e --- /dev/null +++ b/models/analyze.py @@ -0,0 +1,28 @@ +# Import image library necessary for file +import SimpleCV +import sys +from SimpleCV import Image + +def check_blob_in_direct_path(image_path): + + image = Image(image_path) + # grey = image.grayscale() + + instruction = "forward" + array_bounds_possible_widths = [image.width / 4, image.width / 4 * 3] + shapes_pic_attributes = image.size() + + shapes_pic_size = shapes_pic_attributes[0] * shapes_pic_attributes[1] + # dist = img.colorDistance(SimpleCV.Color.Black).dilate(2) + blobs = image.findBlobs() + + #check if the blob is in the middle 1/2 of screen and is too high + for blob in blobs[:-1]: + blob.draw(color=(255,0,0)) + if (blob.coordinates()[0] > array_bounds_possible_widths[0] and blob.coordinates()[0] < array_bounds_possible_widths[1]) and (blob.height() > image.height / 5 and blob.coordinates()[1] > image.height / 5 * 2): + # print grey.height + blob.draw(color=(255,0,0)) + instruction = "stop" + + return instruction + diff --git a/models/analyze.pyc b/models/analyze.pyc new file mode 100644 index 0000000..64bdfa1 Binary files /dev/null and b/models/analyze.pyc differ diff --git a/models/analyzeLine.py b/models/analyzeLine.py new file mode 100644 index 0000000..726e48b --- /dev/null +++ b/models/analyzeLine.py @@ -0,0 +1,26 @@ +import SimpleCV +import cv2 +import time + + +def findBlockingBlobs(img,blobs): + for blob in blobs: + if blockingBlob(img,blob): + return True + return False + +def blockingBlob(img,blob): + if heightBelowThreshold(img,blob): + if lengthSufficientToBlock(img,blob): + return True + + +def heightBelowThreshold(img,blob): + print "blob's maxY: " + print blob.maxY() + if (blob.maxY() > img.size()[1] * 0.4): + return True + +def lengthSufficientToBlock(img,blob): + if (blob.width() > img.size()[0] * 0.55): + return True diff --git a/models/runAnalyzeLine.py b/models/runAnalyzeLine.py new file mode 100644 index 0000000..32a9786 --- /dev/null +++ b/models/runAnalyzeLine.py @@ -0,0 +1,13 @@ +import SimpleCV +import cv2 +import time +from analyzeLine import * + +def runAnalyzeLines(image): + scvImg = SimpleCV(image) + segmented = scvImg.stretch(160,161) + blob = segmented.findBlobs(minsize=100) + + # print x + # print blob + return findBlockingBlobs(scvImg,blob) \ No newline at end of file diff --git a/models/send.py b/models/send.py index 53a4d59..bda87a9 100644 --- a/models/send.py +++ b/models/send.py @@ -1,7 +1,7 @@ import socket -UDP_IP = "10.50.111.176" -UDP_PORT = 5005 +UDP_IP = "38.140.126.106" +UDP_PORT = 10000 MESSAGE = "Hello, World!" print "UDP target IP:", UDP_IP diff --git a/models/send_2.py b/models/send_2.py new file mode 100644 index 0000000..77bc908 --- /dev/null +++ b/models/send_2.py @@ -0,0 +1,15 @@ +#!/usr/bin/python + +import socket #import socket module + +s = socket.socket() #create a socket object +print "socket made" + +host = '10.98.76.121' #Host i.p +port = 12397 #Reserve a port for your service + +print "before connect" +s.connect((host,port)) +print "after connect" +print s.recv(1024) +s.close \ No newline at end of file diff --git a/models/webpy.py b/models/webpy.py new file mode 100644 index 0000000..e69de29 diff --git a/run_py_car.pyc b/run_py_car.pyc new file mode 100644 index 0000000..e6d3ff7 Binary files /dev/null and b/run_py_car.pyc differ diff --git a/run_rc_car.py b/run_rc_car.py new file mode 100644 index 0000000..46fc896 --- /dev/null +++ b/run_rc_car.py @@ -0,0 +1,63 @@ +import RPi.GPIO as GPIO +from time import sleep + +class PiCar(object): + + def __init__(self): + GPIO.setmode(GPIO.BOARD) + self.pins = {'left' : 11, + 'right' : 18, + 'forward' : 16, + 'backward' : 12 } + for pin_number in self.pins.itervalues(): + GPIO.setup(pin_number,GPIO.OUT) + self.stop() + + + def stop(self): + for pin_number in self.pins.itervalues(): + GPIO.output(pin_number,0) + + def go_forward(self,time): + self.stop() + GPIO.output(self.pins['forward'],1) + sleep(int(time)) + self.stop() + + def go_backward(self,time): + self.stop() + GPIO.output(self.pins['backward'],1) + sleep(int(time)) + self.stop() + + def go_forward_right(self,time): + self.stop() + GPIO.output(self.pins['right'],1) + GPIO.output(self.pins['forward'],1) + sleep(int(time)) + self.stop() + + def go_forward_left(self,time): + self.stop() + GPIO.output(self.pins['left'],1) + GPIO.output(self.pins['forward'],1) + sleep(int(time)) + self.stop() + + def go_backward_right(self,time): + self.stop() + GPIO.output(self.pins['right'],1) + GPIO.output(self.pins['backward'],1) + sleep(int(time)) + self.stop() + + def go_backward_left(self,time): + self.stop() + GPIO.output(self.pins['left'],1) + GPIO.output(self.pins['backward'],1) + sleep(int(time)) + self.stop() + + +picar = PiCar() + diff --git a/run_rc_car.pyc b/run_rc_car.pyc new file mode 100644 index 0000000..aa686fe Binary files /dev/null and b/run_rc_car.pyc differ diff --git a/start_camera.py b/start_camera.py new file mode 100644 index 0000000..674daf8 --- /dev/null +++ b/start_camera.py @@ -0,0 +1,6 @@ +import os + +def start_camera(): + os.system('ssh pi@192.168.2.5 python RaspberryDrive/views/takePicture.py &') + return + diff --git a/start_camera.pyc b/start_camera.pyc new file mode 100644 index 0000000..f636865 Binary files /dev/null and b/start_camera.pyc differ diff --git a/start_server.py b/start_server.py new file mode 100644 index 0000000..4020e03 --- /dev/null +++ b/start_server.py @@ -0,0 +1,6 @@ +import os + +def start_server(): + os.system('ssh pi@192.168.2.4 python python-libs/RaspberryDrive/driving_server.py &') + return + diff --git a/start_server.pyc b/start_server.pyc new file mode 100644 index 0000000..44d3266 Binary files /dev/null and b/start_server.pyc differ diff --git a/takePicture.py b/takePicture.py new file mode 100644 index 0000000..06ee0f1 --- /dev/null +++ b/takePicture.py @@ -0,0 +1,17 @@ +import picamera as p +import os +import time + +os.chdir('/home/pi/Desktop') + +cam = p.PiCamera() +cam.resolution = (320,240) + +x = 0 + +while x < 50: + #os.unlink('greg.jpg') + img = cam.capture('gregTest.jpg') + time.sleep(.25) + #oc.rename('gregTemp.jpg', 'greg.jpg') + x +=1 diff --git a/three.jpg b/three.jpg new file mode 100644 index 0000000..567af5c Binary files /dev/null and b/three.jpg differ diff --git a/views/__init__.py b/views/__init__.py new file mode 100644 index 0000000..93f5256 --- /dev/null +++ b/views/__init__.py @@ -0,0 +1 @@ +__init__.py \ No newline at end of file diff --git a/views/takePicture.py b/views/takePicture.py new file mode 100644 index 0000000..0c3748d --- /dev/null +++ b/views/takePicture.py @@ -0,0 +1,20 @@ +import picamera as p +import os +import time + +print "in take picture" +os.chdir('/home/pi/Desktop') + +cam = p.PiCamera() +cam.resolution = (320,240) + +x = 0 + +while x < 50: + #os.unlink('greg.jpg') + img = cam.capture('gregTest.jpg') + time.sleep(.25) + #oc.rename('gregTemp.jpg', 'greg.jpg') + x +=1 + +exit()