-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg.py
167 lines (130 loc) · 5.01 KB
/
img.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import sys
from nltk.corpus import wordnet
import os
from bs4 import BeautifulSoup
from urllib import urlretrieve
import urllib
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import cv2
import pygame
import xml.etree.ElementTree as ET
from pygame.locals import *
class data :
def __init__(self) :
self.obj=str()
self.item=str()
self.size=(0,0)
self.cmd=str()
def getImage(self,search,nn) :
wn=wordnet.synset(str(search)+'.n.01')
wnid=wn.pos()+str(wn.offset()).zfill(8)
imgt="http://image-net.org/api/text/imagenet.synset.geturls?wnid="+wnid
page=urllib.urlopen(imgt)
soup=BeautifulSoup(page,"lxml")
link=soup.get_text().split()
global n
n=nn
counter=1
os.system("mkdir "+str(search))
for im in link :
if counter<=nn :
try:
pg=urllib.urlopen(im)
urltype=pg.info()
if urltype.type=="image/jpeg" :
path=os.path.join(str(search),"pic"+str(counter)+".jpg")
urllib.urlretrieve(im,path)
counter+=1
else :
continue
except (IOError,UnicodeError) :
continue
else :
break
print "Download Complete"
def delete(self,obj,item) :
del_items=item.split()
for items in del_items :
path_delItem=os.path.join(str(obj),items+".jpg")
os.remove(path_delItem)
def disp_crop(self,crop) :
try :
cv2.imshow("cropped",crop)
cv2.imwrite(path_crop,crop)
except :
print "Something went wrong"
def crop(self,obj,test) :
tree=ET.parse(str(test)+".xml")
root=tree.getroot()
os.system("./imglab -c "+str(test)+".xml "+obj)
os.system("./imglab "+str(test)+".xml")
for val in root.findall('images') :
for dim in val :
file=dim.get("file")
print file
for x in dim :
top=int(x.get("top"))
left=int(x.get("left"))
width=int(x.get("width"))
height=int(x.get("height"))
print top
path=os.path.join(file)
img=cv2.imread(path)
crop=img[top:height,left:width]
cv2.imwrite(path,crop)
#cv2.imshow("cropped",crop)
#cv2.waitkey(0)
#cv2.destroyAllWindows()
def resize(self,obj,item,sizex,sizey) :
resize_item=item.split()
for items in resize_item :
path_resize=os.path.join(str(obj),items+".jpg")
resize=cv2.resize(cv2.imread(path_resize),(sizex,sizey))
cv2.imwrite(path_resize,resize)
if __name__=="__main__" :
dat=data()
flag=True
print "=================TERMINAL===================="
while flag :
cmd=raw_input(">>")
if cmd=="exit" :
flag=False
else :
if cmd=="scrap" :
try :
search=raw_input("")
nn=int(raw_input(""))
dat.getImage(search,nn)
except :
print "Network Error"
if cmd =='delete' :
try :
obj_del=raw_input("enter the folder : ")
item_del=raw_input("enter the image : ")
dat.delete(obj_del,item_del)
except :
print "Invalid input"
elif cmd == 'resize' :
try :
obj_resize=raw_input("enter the folder: ")
item_resize=raw_input("enter the image: ")
sizex=int(raw_input("width"))
sizey=int(raw_input("height"))
dat.resize(obj_resize,item_resize,sizex,sizey)
except Exception as e:
print "Invalid input"
print e
elif cmd == "crop" :
try :
obj_crop=raw_input("enter the folder: ")
item=raw_input("enter the xml file name: ")
dat.crop(obj_crop,item)
except Exception as e:
print "Invalid input"
print e
elif cmd == "help" :
print "-------------------COMMANDS---------------------"
print " scrap delete resize crop "
else :
print "invalid command"