Skip to content

Commit

Permalink
Release v2.1 pushed
Browse files Browse the repository at this point in the history
  • Loading branch information
Loïc Vanden Bemden committed Jan 27, 2020
1 parent 6501d09 commit fe0ce3d
Show file tree
Hide file tree
Showing 58 changed files with 1,795 additions and 1,223 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
# Blob Simulation & Detection
Détection d'un blob dans une image ainsi que détection des comprimés de nourriture. Simulation d'un blob dans PyGame à travers un système multi-agents à connaissance partagée.

## Release 2.1 - *7/06/2019*

### Modifications :
+ Déplacement des scripts dans le dossier root
+ Changement du nom des fichiers enregistrés par la détection
+ Automatisation des différents scripts, ajout d'un fonctionnement en mode "caché".
+ Automatisation de la simulation avec ajouts de paramètres et sauvegarde finale
+ Ajout d'un paramètre pour initialiser de la nourriture dans la simulation
+ Ajout d'un paramètre d'affichage dans la simulation
+ Uniformisation des paramètres dans les différents scripts
+ Possibilité de raffiner un modèle détecté avec un fichier d'informations supplémentaires
+ Variables de couleur déplacées dans un fichier json séparé
+ Ajout de fichiers d'exemples pour les différents scripts

### Scripts :
+ Setup : `python setup.py -i data/example.jpg`
+ Detection : `python detect.py -i data/example.jpg`
+ Simulation : `python play.py -i data/output-examples/example-detect.board -s 3`
+ Compare : `python compare.py --first data/output-examples/simulation/10_loops/10_loops.board --second data/output-examples/simulation/100_loops/100_loops.board -s 3`

Les différentes sorties produites sont les suivantes :
![Sortie du script de détection](data/output-examples/example-detect-details.jpg?raw=true "Sortie du script de détection")
![Sortie de la simulation après 100 itérations](data/output-examples/simulation/100_loops/100_loops.jpg?raw=true "Sortie de la simulation après 100 itérations")
![Sortie du script de comparaison](data/compare_init_with_100.jpg?raw=true "Sortie du script de comparaison")


## Release 2.0 - *27/05/2019*

Le passage à cette release invalide les fichiers de sauvegarde précédents, le format ayant été modifié.
Expand All @@ -17,7 +43,6 @@ La nouvelle interface de la simulation produira des images comme celle-ci :

![Interface de simulation](example/test-run.jpg?raw=true "Interface de simulation")


Pour exécuter le script de comparaison sur un exemple :
`python compare.py --first example/test.board --second example/test-run.board`

Expand Down
47 changes: 24 additions & 23 deletions compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,44 @@
import argparse
from simulation import board
import pygame
from pygame.locals import QUIT
from pygame.locals import QUIT, KEYDOWN, K_ESCAPE


def main():
ap = argparse.ArgumentParser()
ap.add_argument("--first", required=True, help="first board file")
ap.add_argument("--second", required=True, help="second board file")
ap.add_argument("-s", "--scale", type=float, default=10, help="scale display (default: x10")
ap.add_argument("-o", "--output", type=str, help="give a name to save the jpeg file")
ap.add_argument("-s", "--scale", type=float, default=10,
help="Scales board resolution by this factor (default: x10)")
ap.add_argument("-o", "--output", type=str, help="Give a name to save the jpeg file")
args = ap.parse_args()

board_1 = board.Board(0,0)
board_1 = board.Board(0, 0)
board_1.load(args.first)

board_2 = board.Board(0,0)
board_2 = board.Board(0, 0)
board_2.load(args.second)

board_comp = board_1.compare(board_2)

if board_comp is None:
return

pygame.init()

width = int(board_1.width * args.scale)
height = int(board_1.height * args.scale)
window = pygame.display.set_mode((width, height))

game_surface = pygame.Surface((board_1.width, board_1.height))
pixel_array = pygame.PixelArray(game_surface)
for x in range(board_1.width):
for y in range(board_1.height):
pixel_array[x,y] = (0, 0, 0)
pixel_array[x, y] = (0, 0, 0)

val = max(-255, min(board_comp.get_blob(x, y), 255))
if val < 0:
val = - val # int(-val/2) + 125
val = - val # int(-val/2) + 125
pixel_array[x, y] = (val/4, val/4, val)
elif val > 0:
val = val # int(val/2) + 125
val = val # int(val/2) + 125
pixel_array[x, y] = (val, val/4, val/4)
else:
if not board_comp.is_touched(x, y):
Expand All @@ -66,24 +64,27 @@ def main():
pixel_array[x, y] = (125, 75, 75)

if board_comp.has_food(x, y):
pixel_array[x, y] = (0, board_comp.foods[x, y], 0)
pixel_array[x, y] = (0, board_comp.foods[x, y], 0)

del pixel_array

game_window = pygame.transform.scale(game_surface, (width, height))

window.blit(game_window, (0, 0))
pygame.display.flip()

ended = False
while not ended:
pygame.time.wait(10)
for event in pygame.event.get():
if event.type == QUIT:
ended = True

if args.output is not None:
pygame.image.save(window, args.output)
pygame.image.save(game_window, args.output)

else:
pygame.init()
window = pygame.display.set_mode((width, height))
window.blit(game_window, (0, 0))
pygame.display.flip()

ended = False
while not ended:
pygame.time.wait(10)
for event in pygame.event.get():
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
ended = True


if __name__ == "__main__":
Expand Down
Binary file added data/_MG_0001.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 added data/_MG_0132.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 added data/_MG_0133.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 added data/_MG_0207.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 added data/compare_init_with_100.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 added data/example.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 added data/output-examples/example-detect-details.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
161 changes: 161 additions & 0 deletions data/output-examples/example-detect.board

Large diffs are not rendered by default.

Binary file added data/output-examples/example-detect.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"clean_top": true,
"food_size": 5,
"food_size": 6,
"use_food_circle": true
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
161 changes: 161 additions & 0 deletions data/output-examples/refined/example-detect.board

Large diffs are not rendered by default.

Binary file added data/output-examples/refined/example-detect.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions data/output-examples/refined/example-detect.player.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"clean_top": false,
"food_size": 6,
"use_food_circle": true
}
32 changes: 32 additions & 0 deletions data/output-examples/simulation/100_loops/100_loops.blob.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"Computing": {
"Blob Size Factor": 0.25,
"Covering Factor": 0.75,
"Global Factor": 2.78,
"Known Foods Factor": 0.05
},
"Gathering": {
"Diagonal Moves": true,
"Light Compute": true,
"Sightline": -1
},
"Global Decrease": 0.1,
"Harvesting": {
"Collect": 10,
"Eat": 5,
"Max": 30,
"Min": 30
},
"Remaining Blob on Food": 50,
"Scouters": {
"Drop by eat": 25,
"Min": 2
},
"Scouting": {
"Diagonal Moves": true,
"Global Explore Probability": 0.02,
"Light Compute": true,
"Search Locally on Food": true,
"Sightline": 3
}
}
161 changes: 161 additions & 0 deletions data/output-examples/simulation/100_loops/100_loops.board

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"clean_top": true,
"food_size": 5,
"food_size": 6,
"use_food_circle": true
}
10 changes: 10 additions & 0 deletions data/output-examples/simulation/100_loops/100_loops.results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Covering": {
"Bottom": 34.74375,
"Top": 9.35,
"Total": 22.046875
},
"From": "save/example-detect",
"Loops": 100,
"To": "save/2019-06-07_16.26.50"
}
32 changes: 32 additions & 0 deletions data/output-examples/simulation/10_loops/10_loops.blob.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"Computing": {
"Blob Size Factor": 0.25,
"Covering Factor": 0.75,
"Global Factor": 2.78,
"Known Foods Factor": 0.05
},
"Gathering": {
"Diagonal Moves": true,
"Light Compute": true,
"Sightline": -1
},
"Global Decrease": 0.1,
"Harvesting": {
"Collect": 10,
"Eat": 5,
"Max": 30,
"Min": 30
},
"Remaining Blob on Food": 50,
"Scouters": {
"Drop by eat": 25,
"Min": 2
},
"Scouting": {
"Diagonal Moves": true,
"Global Explore Probability": 0.02,
"Light Compute": true,
"Search Locally on Food": true,
"Sightline": 3
}
}
161 changes: 161 additions & 0 deletions data/output-examples/simulation/10_loops/10_loops.board

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions data/output-examples/simulation/10_loops/10_loops.player.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"clean_top": true,
"food_size": 6,
"use_food_circle": true
}
10 changes: 10 additions & 0 deletions data/output-examples/simulation/10_loops/10_loops.results.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Covering": {
"Bottom": 33.6875,
"Top": 6.809375000000001,
"Total": 20.2484375
},
"From": "save/example-detect",
"Loops": 10,
"To": "save/10_loops"
}
12 changes: 12 additions & 0 deletions data/refine-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Width": 100,
"Height": 40,
"Foods": [
[10, 10],
[18,28],
[23, 8],
[44,23],
[96,22]
],
"Clean Top": false
}
64 changes: 64 additions & 0 deletions detect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright (C) 2019 - UMons
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import argparse
import json
from detection.detection import detect, discretize, print_results
from detection.refine import simulate, save
from os.path import splitext, basename, join


def main():
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True, help="Uses this input as image for detection")
ap.add_argument("-s", "--scale", type=float, default=0.10, help="Scales images by this factor (default: x0.1)")
ap.add_argument("-c", "--config", type=str, default="detection/config.json",
help="Loads config from this file (default: detection/config.json)")
ap.add_argument("--save", type=str, default="save/",
help="Pass the directory where saves are stored. (default: save/)")
ap.add_argument("--hide", action='store_true', default=False, help="Hide images if parameter is set")
ap.add_argument("--refine", type=str, help="Pass a json file to refine model")

args = ap.parse_args()
with open(args.config, 'r') as file:
config = json.load(file)

if args.refine is not None:
with open(args.refine, 'r') as file:
refine = json.load(file)
else:
refine = None

orig, blob_mask, blob, food_mask, food_img = detect(args.input, config)
dsc_img, dsc_blob, dsc_food_list = discretize(blob, food_mask, config['Discrete Width'], config['Discrete Height'])

if args.save is not None:
filename = splitext(basename(args.input))[0] + "-detect"
file_path = join(args.save, filename)

board, player, img = simulate(dsc_img, dsc_blob, dsc_food_list, config, refine)
save(file_path, board, player, img)

# Prepare file_path for details if any to save
file_path += "-details"
else:
file_path = None

print_results(orig, blob_mask, blob, food_mask, food_img, dsc_img, args.scale, file_path, args.hide)


if __name__ == "__main__":
main()
28 changes: 20 additions & 8 deletions detection/config.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
{
"Aspect Ratio": 0.4,
"Discrete Height": 240,
"Discrete Width": 600,
"Discrete Height": 160,
"Discrete Width": 400,
"High Food Color": [
214,
230,
237
],
"Limits": [
[136, 268],
[5484, 208],
[5452, 3296],
[136, 3308]
],
[
136,
268
],
[
5484,
208
],
[
5452,
3296
],
[
136,
3308
]
],
"Low Food Color": [
150,
185,
198
],
"Min Food Size": 60
"Min Food Size": 60
}
Loading

0 comments on commit fe0ce3d

Please sign in to comment.