- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathteach_classificationbox.py
More file actions
Latest commit
86 lines (70 loc) · 2.9 KB
/
Copy pathteach_classificationbox.py
File metadata and controls
86 lines (70 loc) · 2.9 KB
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
"""
Script to teach classificationbox image classes as described in
https://blog.machinebox.io/how-anyone-can-build-a-machine-learning-image-classifier-from-photos-on-your-hard-drive-very-5c20c6f2764f
https://github.com/machinebox/toys/blob/master/imgclass/main.go
Run from within the root folder containing the folders of images.
"""
importos
importrequests
IP='localhost'
PORT='8080'
CLASSIFIER='classificationbox'
VALID_FILETYPES= ('.jpg', '.png', '.jpeg')
TEACH_URL=f"http://{IP}:{PORT}/{CLASSIFIER}/teach"
HEALTH_URL=f"http://{IP}:{PORT}/readyz"
defcheck_classifier_health():
"""Check that classifier is reachable"""
try:
response=requests.get(HEALTH_URL)
ifresponse.status_code==200:
print("{} health-check passed".format(CLASSIFIER))
returnTrue
else:
print("{} health-check failed".format(CLASSIFIER))
print(response.status_code)
returnFalse
exceptrequests.exceptions.RequestExceptionasexception:
print("{} is unreachable".format(CLASSIFIER))
print(exception)
deflist_folders(directiory='.'):
"""Returns a list of folders in a dir, defaults to current dir.
These are not full paths, just the folder."""
folders= [dirfordirinos.listdir(directiory)
ifos.path.isdir(os.path.join(directiory, dir))
andnotdir.startswith(directiory)
andnotdir.startswith('.')]
folders.sort(key=str.lower)
print("Folders found:")
print(folders)
returnfolders
defteach_name_by_file(teach_url, name, file_path):
"""Teach facebox a single name using a single file."""
file_name=file_path.split("/")[-1]
file= {'file': open(file_path, 'rb')}
data= {'name': name, "id": file_name}
response=requests.post(teach_url, files=file, data=data)
ifresponse.status_code==200:
print("File:{} taught with name:{}".format(file_name, name))
returnTrue
elifresponse.status_code==400:
print("Teaching of file:{} failed with message:{}".format(
file_name, response.text))
returnFalse
elifresponse.status_code==404:
print("Teaching of file:{} failed due to :{}".format(
file_name, response.text))
returnFalse
defmain():
ifcheck_classifier_health():
forfolder_nameinlist_folders():
folder_path=os.path.join(os.getcwd(), folder_name)
print(f"Entering folder {folder_path}")
forfileinos.listdir(folder_path):
iffile.endswith(VALID_FILETYPES):
file_path=os.path.join(folder_path, file)
print(file_path)
teach_name_by_file(teach_url=TEACH_URL,
name=folder_name,
file_path=file_path)
if__name__=='__main__':
main()