projectstreet-detection-docker

Docker for detecting streets. (WIP)
git clone git://archive.git.mtrnord.blog/Nordgedanken/projectstreet-detection-docker.git
Log | Files | Refs | README

download-image-by-link.py (5163B)


      1 ##########################################################################################################
      2 # Code is based on: https://pythonprogramming.net/haar-cascade-object-detection-python-opencv-tutorial/  #
      3 ##########################################################################################################
      4 import urllib2
      5 from urllib2 import HTTPError, URLError
      6 import cv2
      7 import numpy as np
      8 import os
      9 from socket import timeout
     10 import argparse
     11 
     12 parser = argparse.ArgumentParser()
     13 parser.add_argument('--stage', help='Stage to run')
     14 args = parser.parse_args()
     15 
     16 def store_raw_images():
     17     neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n00523513'   
     18     neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()
     19     pic_num = 1
     20     fail_images = 0
     21     fail_url = 0
     22     
     23     if not os.path.exists('neg'):
     24         os.makedirs('neg')
     25         
     26     for i in neg_image_urls.split('\n'):
     27         try:
     28             i
     29             try:
     30                 f = urllib2.urlopen(i)
     31                 data = f.read()
     32                 with open("neg/"+str(pic_num)+".jpg", "wb") as code:
     33                     code.write(data)
     34             except (HTTPError, URLError) as error:
     35                 fail_url = 0
     36             except timeout:
     37                 fail_url = 0
     38             img = cv2.imread("neg/"+str(pic_num)+".jpg",cv2.IMREAD_GRAYSCALE)
     39             if img is not None:
     40                 # should be larger than samples / pos pic (so we can place our image on it)
     41                 resized_image = cv2.resize(img, (100, 100))
     42                 cv2.imwrite("neg/"+str(pic_num)+".jpg",resized_image)
     43             else:
     44                 fail_images += 1
     45             pic_num += 1
     46             
     47         except Exception as e:
     48             print str(e)
     49     print fail_images + " invalid imgages"
     50     print fail_url + " invalid urls"
     51             
     52 def store_raw_images2():
     53     neg_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n07942152'   
     54     neg_image_urls = urllib2.urlopen(neg_images_link).read().decode()
     55     pic_num = len([name for name in os.listdir('neg') if os.path.isfile(name)])+1
     56     fail_images = 0
     57     fail_url = 0
     58     
     59     if not os.path.exists('neg'):
     60         os.makedirs('neg')
     61         
     62     for i in neg_image_urls.split('\n'):
     63         try:
     64             i
     65             try:
     66                 f = urllib2.urlopen(i)
     67                 data = f.read()
     68                 with open("neg/"+str(pic_num)+".jpg", "wb") as code:
     69                     code.write(data)
     70             except (HTTPError, URLError) as error:
     71                 fail_url = 0
     72             except timeout:
     73                 fail_url = 0
     74             img = cv2.imread("neg/"+str(pic_num)+".jpg",cv2.IMREAD_GRAYSCALE)
     75             if img is not None:
     76                 # should be larger than samples / pos pic (so we can place our image on it)
     77                 resized_image = cv2.resize(img, (100, 100))
     78                 cv2.imwrite("neg/"+str(pic_num)+".jpg",resized_image)
     79             else:
     80                 fail_images += 1
     81             pic_num += 1
     82             
     83         except Exception as e:
     84             print str(e)
     85     print fail_images + " invalid imgages"
     86     print fail_url + " invalid urls"
     87     
     88 def store_raw_pos_images():
     89     pos_images_link = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n04096066'   
     90     pos_image_urls = urllib2.urlopen(pos_images_link).read().decode()
     91     pic_num = len([name for name in os.listdir('pos') if os.path.isfile(name)])+1
     92     fail_images = 0
     93     fail_url = 0
     94     
     95     if not os.path.exists('pos'):
     96         os.makedirs('pos')
     97         
     98     for i in pos_image_urls.split('\n'):
     99         try:
    100             i
    101             try:
    102                 f = urllib2.urlopen(i)
    103                 data = f.read()
    104                 with open("pos/"+str(pic_num)+".jpg", "wb") as code:
    105                     code.write(data)
    106             except (HTTPError, URLError) as error:
    107                 fail_url = 0
    108             except timeout:
    109                 fail_url = 0
    110             img = cv2.imread("pos/"+str(pic_num)+".jpg")
    111             if img is not None:
    112                 # should be larger than samples / pos pic (so we can place our image on it)
    113                 resized_image = cv2.resize(img, (50, 50))
    114                 cv2.imwrite("pos/"+str(pic_num)+".jpg",resized_image)
    115             else:
    116                 fail_images += 1
    117             pic_num += 1
    118             
    119         except Exception as e:
    120             print str(e)
    121     print fail_images + " invalid imgages"
    122     print fail_url + " invalid urls"
    123 
    124 def create_pos_n():
    125     for file_type in ['neg','pos']:
    126         
    127         for img in os.listdir(file_type):
    128 
    129             if file_type == 'pos':
    130                 line = file_type+'/'+img+' 1 0 0 50 50\n'
    131                 with open('info.dat','a') as f:
    132                     f.write(line)
    133             elif file_type == 'neg':
    134                 line = file_type+'/'+img+'\n'
    135                 with open('bg.txt','a') as f:
    136                     f.write(line)
    137                     
    138 
    139 if args.stage == "1_1":
    140     store_raw_images()
    141 if args.stage == "1_2":
    142     store_raw_images2()
    143 if args.stage == "1_3":
    144     store_raw_pos_images()
    145     create_pos_n()