From 21f2d473cf49d1315c743ba7dd65c33cc2c6c90f Mon Sep 17 00:00:00 2001 From: Vale Date: Tue, 5 May 2015 07:00:32 -0400 Subject: [PATCH] Miscellaneous bug fixes and updates - Added if main statements to most class files - Updated current test parser to be more efficient and to pull all shapes out of a picture - Updated parser; now pseudocode is complete, working on migrating that to runnable code --- clear_unwanted_files.py | 5 ++++- download_image.py | 5 ++++- gpio_shooting.py | 5 ++++- image_parser.py | 21 +++++++++++------- image_testing.py | 48 +++++++++++++++++++++++++++++------------ shoot_image.py | 9 +++++--- 6 files changed, 65 insertions(+), 28 deletions(-) diff --git a/clear_unwanted_files.py b/clear_unwanted_files.py index 3cbcfddf..e4c2c0ce 100644 --- a/clear_unwanted_files.py +++ b/clear_unwanted_files.py @@ -16,4 +16,7 @@ def empty_directory( directory_name, file_name ): #if filename contains file_name: if file_name in filename: #delete filename - dirs.remove( file_name ) \ No newline at end of file + dirs.remove( file_name ) + +if __name__ == '__main__': + pass \ No newline at end of file diff --git a/download_image.py b/download_image.py index 79aab5db..203bc078 100644 --- a/download_image.py +++ b/download_image.py @@ -18,4 +18,7 @@ def download_most_recent_image(): if not stderr: return true else: - return false \ No newline at end of file + return false + +if __name__ == '__main__': + pass \ No newline at end of file diff --git a/gpio_shooting.py b/gpio_shooting.py index f32020d3..e2fdf3e9 100644 --- a/gpio_shooting.py +++ b/gpio_shooting.py @@ -26,4 +26,7 @@ def get_shot_truefalse(): if GPIO.input( inPin ): return true else: - return false \ No newline at end of file + return false + +if __name__ == '__main__': + pass \ No newline at end of file diff --git a/image_parser.py b/image_parser.py index 6ea77717..9c088aac 100644 --- a/image_parser.py +++ b/image_parser.py @@ -1,20 +1,25 @@ __author__ = 'Vale Tolpegin' -class image_parser: - #image that needs to be parsed variable +import numpy as np +import cv2 +class image_parser: def __init__( self, *args, **kwargs ): pass - def parse( picture_file ): + def parse( picture_file_name ): #set image variable here with the image file picture_file #find shapes in image ( basic geometric shapes such as circles, squares, triangles, etc ) #inside of those geometric shapes, find the number of different colors - #if there are 2 colors ( no more and no less ) - #compare the ratio of those 2 colors - #if that ratio is close to 2:1 ( shape:letter ) - #return true + #if there are 2 colors ( other than black, no more and no less ) + #if there are 2 distinct seperate colors ( other than black ) within a couple of pixels of each other + #compare the ratio of those 2 colors ( non black ) + #if that ratio is close to 2:1 ( shape:letter ) + #return True + + return false - #return false \ No newline at end of file +if __name__ == '__main__': + pass \ No newline at end of file diff --git a/image_testing.py b/image_testing.py index e359cd26..33d979d5 100644 --- a/image_testing.py +++ b/image_testing.py @@ -1,45 +1,65 @@ import numpy as np import cv2 -img = cv2.imread( 'images/IMG_0116.jpg' ) -img2 = cv2.imread( 'images/IMG_0116.jpg', 0 ) +img = cv2.imread( 'images/IMG_0119.jpg' ) +img2 = cv2.imread( 'images/IMG_0119.jpg', 0 ) ret,thresh = cv2.threshold(img2,127,255,0) contours,h = cv2.findContours(thresh,1,2) +def img_copy( cnt ): + mask_img = np.zeros( img.shape, dtype=np.uint8 ) + roi_corners = np.array( cnt, dtype=np.int32 ) + black = (255, 255, 255) + cv2.drawContours( mask_img, [cnt], 0, black, -1 ) + + masked_img = cv2.bitwise_and( img, mask_img ) + + cv2.imshow( "Image", masked_img ) + cv2.waitKey(0) + cv2.destroyAllWindows() + + return test_image( masked_img ) + +def test_image( masked_img ): + return True + for cnt in contours: approx = cv2.approxPolyDP(cnt,0.007*cv2.arcLength(cnt,True),True) if cv2.contourArea(cnt) < 20000: pass else: - if len(approx)==9: - mask_img = np.zeros( img.shape, dtype=np.uint8 ) - roi_corners = np.array( cnt, dtype=np.int32 ) - white = (255, 255, 255) - cv2.drawContours( mask_img, [cnt], 0, white, -1 ) - - masked_img = cv2.bitwise_and( img, mask_img ) - - cv2.imshow( "Image", masked_img ) - cv2.waitKey(0) - cv2.destroyAllWindows() - print len(approx) if len(approx)==5: print "pentagon" + + img_copy( cnt ) + cv2.drawContours(img,[cnt],0,255,-1) elif len(approx)==3: print "triangle" + + img_copy( cnt ) + cv2.drawContours(img,[cnt],0,(0,255,0),-1) elif len(approx)==4: print "square" + + img_copy( cnt ) + cv2.drawContours(img,[cnt],0,(0,0,255),-1) elif len(approx)==5: print "pentagon" + + img_copy( cnt ) + cv2.drawContours(img,[cnt],0,(0,0,127),-1) elif len(approx) == 9: print "complex" + + img_copy( cnt ) + cv2.drawContours(img,[cnt],0,(255,255,0),-1) cv2.imshow( "Image", img ) diff --git a/shoot_image.py b/shoot_image.py index 349d9dcf..6b530531 100644 --- a/shoot_image.py +++ b/shoot_image.py @@ -1,7 +1,7 @@ __author__ = 'Vale Tolpegin' import os -import download_image from download_image +import download_image import subprocess class shoot_image: @@ -23,5 +23,8 @@ def shoot_an_image(): download.download_most_recent_image() return True - else - return False \ No newline at end of file + else: + return False + +if __name__ == '__main__': + pass \ No newline at end of file