Skip to content

Commit

Permalink
Merge pull request #9 from Unimakers/dev
Browse files Browse the repository at this point in the history
add: ML Pipeline - Robovision
  • Loading branch information
amaghzaz-y authored Dec 19, 2023
2 parents 9e92cd6 + d5ea1dd commit b081305
Show file tree
Hide file tree
Showing 110 changed files with 248 additions and 84 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
.vscode/ipch
.vscode
.cache
docs/site
docs/site

__pycache__
Binary file removed assets/logo.png
Binary file not shown.
Binary file modified ml/data/plant/0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/11.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/12.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/13.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/14.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/15.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/16.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/17.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/18.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/19.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/20.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/21.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/22.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/23.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/24.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/25.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/26.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/27.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified ml/data/plant/6.jpg
Binary file modified ml/data/plant/7.jpg
Binary file modified ml/data/plant/8.jpg
Binary file modified ml/data/plant/9.jpg
Binary file modified ml/data/pot/0.jpg
Binary file modified ml/data/pot/1.jpg
Binary file modified ml/data/pot/10.jpg
Binary file modified ml/data/pot/11.jpg
Binary file modified ml/data/pot/12.jpg
Binary file modified ml/data/pot/13.jpg
Binary file modified ml/data/pot/14.jpg
Binary file modified ml/data/pot/15.jpg
Binary file modified ml/data/pot/16.jpg
Binary file modified ml/data/pot/17.jpg
Binary file modified ml/data/pot/18.jpg
Binary file modified ml/data/pot/19.jpg
Binary file modified ml/data/pot/2.jpg
Binary file modified ml/data/pot/20.jpg
Binary file modified ml/data/pot/21.jpg
Binary file modified ml/data/pot/22.jpg
Binary file modified ml/data/pot/23.jpg
Binary file modified ml/data/pot/24.jpg
Binary file modified ml/data/pot/25.jpg
Binary file modified ml/data/pot/26.jpg
Binary file modified ml/data/pot/27.jpg
Binary file modified ml/data/pot/28.jpg
Binary file modified ml/data/pot/29.jpg
Binary file modified ml/data/pot/3.jpg
Binary file modified ml/data/pot/30.jpg
Binary file modified ml/data/pot/31.jpg
Binary file modified ml/data/pot/32.jpg
Binary file modified ml/data/pot/33.jpg
Binary file modified ml/data/pot/34.jpg
Binary file modified ml/data/pot/35.jpg
Binary file modified ml/data/pot/36.jpg
Binary file modified ml/data/pot/37.jpg
Binary file modified ml/data/pot/38.jpg
Binary file modified ml/data/pot/39.jpg
Binary file modified ml/data/pot/4.jpg
Binary file modified ml/data/pot/40.jpg
Binary file modified ml/data/pot/41.jpg
Binary file modified ml/data/pot/42.jpg
Binary file modified ml/data/pot/43.jpg
Binary file modified ml/data/pot/5.jpg
Binary file modified ml/data/pot/6.jpg
Binary file modified ml/data/pot/7.jpg
Binary file modified ml/data/pot/8.jpg
Binary file modified ml/data/pot/9.jpg
Binary file added ml/data/random/0.jpg
Binary file added ml/data/random/1.jpg
Binary file added ml/data/random/10.jpg
Binary file added ml/data/random/11.jpg
Binary file added ml/data/random/12.jpg
Binary file added ml/data/random/13.jpg
Binary file added ml/data/random/14.jpg
Binary file added ml/data/random/15.jpg
Binary file added ml/data/random/16.jpg
Binary file added ml/data/random/17.jpg
Binary file added ml/data/random/18.jpg
Binary file added ml/data/random/19.jpg
Binary file added ml/data/random/2.jpg
Binary file added ml/data/random/20.jpg
Binary file added ml/data/random/21.jpg
Binary file added ml/data/random/3.jpg
Binary file added ml/data/random/4.jpg
Binary file added ml/data/random/5.jpg
Binary file added ml/data/random/6.jpg
Binary file added ml/data/random/7.jpg
Binary file added ml/data/random/8.jpg
Binary file added ml/data/random/9.jpg
Binary file added ml/test/1.jpg
Binary file added ml/test/2.jpg
Binary file added ml/test/3.jpg
Binary file added ml/test/4.jpg
146 changes: 111 additions & 35 deletions ml/training.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import time
import cv2
from sklearn import svm
import os
from PIL import Image
from skimage.feature import hog
from skimage import exposure
import pickle

x: list[int | float] = []
y: list[str] = []

cell_per_block = (1, 1)
pixels_per_cell = (8, 8)
window_size = 8


def read_blobs():
plants = []
Expand All @@ -24,76 +28,148 @@ def read_blobs():
def get_data():
plants = []
pots = []
random = []
for filename in os.listdir("data/plant"):
file_path = os.path.join("data/plant", filename)
plants.append(file_path)
for filename in os.listdir("data/pot"):
file_path = os.path.join("data/pot", filename)
pots.append(file_path)
return (plants, pots)
for filename in os.listdir("data/random"):
file_path = os.path.join("data/random", filename)
random.append(file_path)
return (plants, pots, random)


def normalize_data():
plants, pots = read_blobs()
plants, pots, random = get_data()
for index, url in enumerate(plants):
img = Image.open(url)
img = img.resize((64, 64))
if img.height == 32:
img.close()
continue
img = img.resize((32, 32))
img.save(f"data/plant/{index}.jpg")
for index, url in enumerate(pots):
img = Image.open(url)
img = img.resize((64, 64))
if img.height == 32:
img.close()
continue
img = img.resize((32, 32))
img.save(f"data/pot/{index}.jpg")
for index, url in enumerate(random):
img = Image.open(url)
if img.height == 32:
img.close()
continue
img = img.resize((32, 32))
img = img.convert("RGB")
img.save(f"data/random/{index}.jpg")


def create_dataframe():
global x, y
plants, pots = get_data()
for index, url in enumerate(plants):
plants, pots, random = get_data()
for url in plants:
img = Image.open(url)
fd = hog(
img,
orientations=8,
pixels_per_cell=(16, 16),
cells_per_block=(1, 1),
pixels_per_cell=pixels_per_cell,
cells_per_block=cell_per_block,
visualize=True,
channel_axis=-1,
)
x.append(fd[0])
y.append("plant")
for index, url in enumerate(pots):
for url in pots:
img = Image.open(url)
fd = hog(
img,
orientations=8,
pixels_per_cell=(16, 16),
cells_per_block=(1, 1),
pixels_per_cell=pixels_per_cell,
cells_per_block=cell_per_block,
visualize=True,
channel_axis=-1,
)
x.append(fd[0])
y.append("pot")
for url in random:
img = Image.open(url)
fd = hog(
img,
orientations=8,
pixels_per_cell=pixels_per_cell,
cells_per_block=cell_per_block,
visualize=True,
channel_axis=-1,
)
x.append(fd[0])
y.append("object")


normalize_data()
create_dataframe()
clf = svm.SVC()
clf.fit(x, y)

img = Image.open("../dataset/blobs/pots/50.pot.jpg")
img = img.resize((64, 64))
fd = hog(
img,
orientations=8,
pixels_per_cell=(16, 16),
cells_per_block=(1, 1),
visualize=True,
channel_axis=-1,
)


res = clf.predict([fd[0]])
print(res)


# save model
with open("model.pkl", "wb") as f:
pickle.dump(clf, f)
clf = svm.SVC(decision_function_shape="ovo", kernel="rbf", C=200, probability=True)
clf.fit(x, y) # type:ignore
# with open("model.pkl", "rb") as f:
# clf: svm.SVC = pickle.load(f)
img = Image.open("test/4.jpg")
imgcv = cv2.imread("test/4.jpg")

now = time.time()

xa = 0
ya = 0


def get_value(x: list):
plant = x[0][0]
pot = x[0][1]
obj = x[0][2]
if plant > pot and plant > obj:
if plant > 85.0:
return "plant"
if pot > plant and pot > obj:
if pot > 85.0:
return "pot"
if obj > plant and obj > pot:
if obj > 85.0:
return "object"
return None


while xa < 64:
while ya < 64:
window = img.crop((xa, ya, xa + window_size, ya + window_size))
window = window.resize((32, 32))
fd = hog(
window,
orientations=8,
pixels_per_cell=pixels_per_cell,
cells_per_block=cell_per_block,
visualize=True,
channel_axis=-1,
)
res = clf.predict_proba([fd[0]]) # type:ignore
if get_value(res) == "plant": # type:ignore
print(res)
cv2.rectangle(
imgcv, (xa, ya), (xa + window_size, ya + window_size), (255, 0, 0), 1
)
ya += window_size
xa += window_size
continue
# print(f"{xa}:{ya} = {res[0]}")
ya += 1
xa += 1
ya = 0
print(f"time elapsed : { (time.time() - now) *1000 } ms")

cv2.imshow("image", imgcv)
cv2.waitKey(0)


# # save model
# with open("model.pkl", "wb") as f:
# pickle.dump(clf, f)
2 changes: 1 addition & 1 deletion tools/eql.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def tokenize_text():
if keywords.count(tokens[0]) == 0:
print_parse_error(tokens[0])
type = get_token_type(tokens[0])
token = assemble_command(type, tokens)
token = assemble_command(type, tokens) # type: ignore


def get_token_type(token: str):
Expand Down
1 change: 0 additions & 1 deletion tools/requirements.txt

This file was deleted.

Binary file added tools/robovision/assets/logo.png
File renamed without changes
Binary file added tools/robovision/assets/tank.png
37 changes: 37 additions & 0 deletions tools/robovision/components/leftpane.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import dearpygui.dearpygui as dpg


class LeftPane:
height: int | None
width: int

def __init__(self):
self.height = dpg.get_item_height("table")
if self.height is None:
self.height = 720

def register(self):
with dpg.texture_registry(show=False):
width, height, _, data = dpg.load_image("assets/logo.png")
dpg.add_static_texture(
width=width,
height=height,
default_value=data,
tag="logo_texture",
)
with dpg.window(
label="RoboVision 0.0.1",
no_close=True,
pos=[0, 0],
width=200,
height=self.height,
no_collapse=True,
no_move=True,
):
dpg.add_image(
"logo_texture",
width=185,
height=32,
tag="logo_img",
)
dpg.add_text(source="mouse_pos")
39 changes: 39 additions & 0 deletions tools/robovision/components/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import dearpygui.dearpygui as dpg

TABLE_WIDTH = 3000
TABLE_HEIGHT = 2000
TABLE_RATIO = 0.3
TABLE_VIEW_WIDTH = (int)(TABLE_WIDTH * TABLE_RATIO)
TABLE_VIEW_HEIGHT = (int)(TABLE_HEIGHT * TABLE_RATIO)
TABLE_REAL_RATIO = TABLE_WIDTH / TABLE_VIEW_WIDTH
MARGIN_OFFSET = 30
robot_pos = [0, 0]
robot_size = [450 * TABLE_RATIO, 450 * TABLE_RATIO]


class Table2D:
def register(self):
width, height, _, data = dpg.load_image("assets/table.png")
with dpg.texture_registry(show=False):
dpg.add_static_texture(
width=width,
height=height,
default_value=data,
tag="table_texture",
)
with dpg.window(
label="Table 2D",
tag="table",
pos=[205, 0],
height=640,
no_close=True,
no_move=True,
no_resize=True,
no_collapse=True,
):
dpg.add_image(
"table_texture",
width=TABLE_VIEW_WIDTH,
height=TABLE_VIEW_HEIGHT,
tag="table_img",
)
57 changes: 57 additions & 0 deletions tools/robovision/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from math import floor
import dearpygui.dearpygui as dpg
from components.table import Table2D
from components.leftpane import LeftPane


TABLE_WIDTH = 3000
TABLE_HEIGHT = 2000
TABLE_RATIO = 0.3
TABLE_VIEW_WIDTH = (int)(TABLE_WIDTH * TABLE_RATIO)
TABLE_VIEW_HEIGHT = (int)(TABLE_HEIGHT * TABLE_RATIO)
TABLE_REAL_RATIO = TABLE_WIDTH / TABLE_VIEW_WIDTH
MARGIN_OFFSET = 30
robot_pos = [0, 0]
robot_size = [450 * TABLE_RATIO, 450 * TABLE_RATIO]


def main():
dpg.create_context()
dpg.create_viewport(title="RoboVision")
dpg.setup_dearpygui()
dpg.set_viewport_vsync(True)
dpg.toggle_viewport_fullscreen()
register_values()
register_handlers()

table = Table2D()
table.register()
leftpane = LeftPane()
leftpane.register()

dpg.show_viewport()

dpg.start_dearpygui()
dpg.destroy_context()


def handle_mouse_click(sender, data):
if dpg.is_item_hovered("table") and dpg.is_item_focused("table"):
pos = dpg.get_mouse_pos()
x = floor(pos[0] * TABLE_REAL_RATIO) - MARGIN_OFFSET
y = floor(pos[1] * TABLE_REAL_RATIO) - MARGIN_OFFSET
dpg.set_value("mouse_pos", f"MOUSE:{pos}")


def register_handlers():
with dpg.handler_registry():
dpg.add_mouse_move_handler(callback=handle_mouse_click)


def register_values():
with dpg.value_registry():
dpg.add_string_value(tag="mouse_pos", default_value="MOUSE:[0.0,0.0]")


if __name__ == "__main__":
main()
Loading

0 comments on commit b081305

Please sign in to comment.