Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
DONE!
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt committed Feb 10, 2019
1 parent a095f15 commit c12095f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ __pycache__/
db.sqlite3
**/.ipynb_checkpoints
Archive/
*.pyc
10 changes: 9 additions & 1 deletion mask-portal/src/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function Home({ history, match }) {
const [latestImage, setLatestImage] = useState();

useInterval(async () => {
const response = await api.get('/defectpositionimages/?limit=1')
const response = await api.get('/defectpositionimages/')
if (response.data.count > 0 &&
(!latestImage || response.data.results[0].id !== latestImage.id)) {
setLatestImage(response.data.results[0]);
Expand Down Expand Up @@ -70,6 +70,14 @@ export default function Home({ history, match }) {
<p>Image: {latestImage.id}</p>
<p>Defects: {latestImage.defects.length}</p>
<p>Taken {moment(latestImage.create_time).calendar()}</p>
{latestImage.defects.map((d, i) =>
<Fragment>
<h4>Defect {i+1}</h4>
<p>Center: ({d.ellipse.centerX}, {d.ellipse.centerY})</p>
<p>Axis: ({d.ellipse.x}, {d.ellipse.y})</p>
<p>Rotation: {Number((d.ellipse.rotation).toFixed(2))}</p>
</Fragment>
)}
</Fragment>
:
<p>Loading...</p>
Expand Down
143 changes: 76 additions & 67 deletions upload_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,87 +5,96 @@
import json
import cv2
import time
from random import shuffle
from vision.inference import run
from vision.evaluation import evalutate_once, init_dataset, init_model
from vision.geometry import attributes_of_ellipses

from io import BytesIO


BASE_URL = 'http://localhost:8000'

data_dir = "./Archive/photomask_trainingdata"
model_path = "./Archive/SlimWide_weakly_3.pth"

increase_factor = 1.3
current_min = 4
current_max = 6
mask_remaining = random.randint(3, 6)
position_remaining = random.randint(1, 2)
x_position = random.randint(10, 190)
y_position = random.randint(10, 190)
start_mask_response = requests.get(f'{BASE_URL}/masks/')
mask_id = start_mask_response.json()['count'] + 1
model = init_model(model_path)
ds = init_dataset(data_dir)

if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")

BASE_URL = 'http://localhost:8000'

start_mask_response = requests.get(f'{BASE_URL}/masks/')
mask_id = start_mask_response.json()['count'] + 1

image_paths = range(1000)

mask_remaining = random.randint(10, 100)
position_remaining = random.randint(2, 4)
x_position = random.randint(10, 190)
y_position = random.randint(10, 190)
for k in range(len(ds)):
imgs, ellipses, classes = evalutate_once(k, model, ds, device)
#print(imgs)
buf = BytesIO()
buf.write(cv2.imencode('.png', imgs)[1].tostring())

files = {
'image': ('test.png', cv2.imencode('.png', imgs)[1].tostring())
}


data = []
defects = []
if ellipses:
for e in ellipses:
center, axis, angle, _ = attributes_of_ellipses(e)
ellipse = {
"ellipse": {
"centerX": center[0],
"centerY": center[1],
"x": axis[0],
"y": axis[1],
"rotation": angle
}}

defects.append(ellipse)

if position_remaining == 0:
if mask_remaining == 0:
mask_id += 1
mask_remaining = random.randint(10, 100)
mask_remaining -= 1

x_position = random.randint(10, 190)
y_position = random.randint(10, 190)
position_remaining = random.randint(2, 4)
position_remaining -= 1

data = {
"mask_id": mask_id,
"position_x": x_position,
"position_y": y_position,
}
for i, d in enumerate(defects):
data[f'new_defects[{i}]'] = json.dumps(d)

print('~~~~~~~~~~~~~~~~~~~~~~~~Starting Upload~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print(f'data {data}')
#print(f'files {files}')
response = requests.post(f'{BASE_URL}/defectpositionimages/', files=files,
data=data)
print(f'Got response: {response.json()}')
print('~~~~~~~~~~~~~~~~~~~~~~~~Upload Finished~~~~~~~~~~~~~~~~~~~~~~~~~~~')
time.sleep(.5)
while(True):
print('---------------------------------------RESTARTING----------------------------------')
ds = init_dataset(data_dir)

ds_range = [x for x in range(len(ds))]
shuffle(ds_range)
for k in ds_range:
imgs, ellipses, classes = evalutate_once(k, model, ds, device)
#print(imgs)
buf = BytesIO()
buf.write(cv2.imencode('.png', imgs)[1].tostring())

files = {
'image': ('test.png', cv2.imencode('.png', imgs)[1].tostring())
}


data = []
defects = []
if ellipses:
for e in ellipses:
center, axis, angle, _ = attributes_of_ellipses(e)
ellipse = {
"ellipse": {
"centerX": center[0],
"centerY": center[1],
"x": axis[0],
"y": axis[1],
"rotation": angle
}}

defects.append(ellipse)

if position_remaining <= 0:
if mask_remaining <= 0:
mask_id += 1
current_min = int(current_min * increase_factor)
current_max = int(current_max * increase_factor)
mask_remaining = min(random.randint(current_min, current_max),
random.randint(100, 150))
else:
mask_remaining -= 1

x_position = random.randint(10, 190)
y_position = random.randint(10, 190)
position_remaining = random.randint(1, 1)
else:
position_remaining -= 1

data = {
"mask_id": mask_id,
"position_x": x_position,
"position_y": y_position,
}
for i, d in enumerate(defects):
data[f'new_defects[{i}]'] = json.dumps(d)

print('~~~~~~~~~~~~~~~~~~~~~~~~Starting Upload~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print(f'data {data}')
#print(f'files {files}')
response = requests.post(f'{BASE_URL}/defectpositionimages/', files=files,
data=data)
print(f'Got response: {response.json()}')
print('~~~~~~~~~~~~~~~~~~~~~~~~Upload Finished~~~~~~~~~~~~~~~~~~~~~~~~~~~')
time.sleep(.4)

0 comments on commit c12095f

Please sign in to comment.