forked from matthewcrotty/Smart-Traffic-Lights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obtainData.py
53 lines (39 loc) · 1.43 KB
/
obtainData.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
import cv2
import os
import pickle
import shutil
import numpy as np
import json
from pprint import pprint
import urllib.request
from pycocotools.coco import COCO
jsonFolder = "./cocodataset/"
outputfolder = "./cocoimages/"
# with open(jsonFolder+'instances_train2017.json') as data_file:
# data = json.load(data_file)
# print(data.keys())
# print(data["images"][0]);
# print(data["annotations"][0]);
# resp = urllib.request.urlopen(data["images"][0]['coco_url'])
# image = np.asarray(bytearray(resp.read()), dtype="uint8")
# image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# cv2.imshow('image',image)
coco=COCO(jsonFolder+'instances_train2017.json')
# cats = coco.loadCats(coco.getCatIds())
# nms=[cat['name'] for cat in cats]
# print('COCO categories: \n{}\n'.format(' '.join(nms)))
catIds = coco.getCatIds(catNms=['person'])
catIds2 = coco.getCatIds(catNms=['vehicle'])
imgIds = coco.getImgIds(catIds=catIds ) + coco.getImgIds(catIds=catIds2 )
imgs = coco.loadImgs(imgIds)
for img in imgs:
resp = urllib.request.urlopen(img['coco_url'])
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imwrite(outputfolder+str(img['id'])+'.png',image)
edges = cv2.Canny(image,100,100)
cv2.imwrite(outputfolder+"e"+str(img['id'])+'.png',edges)
# annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
# anns = coco.loadAnns(annIds)
# print(anns)
# coco.showAnns(anns);