An app written in Python (Flask) that enables you to recognize π constellations on a static picture, using HAAR cascade π€.
To run this app you'll need to install python 3.10.x.
Clone the repo using the following command:
git clone [email protected]:marinmaslov/constellano.gitPosition yourself into the constellano directory:
cd constellanoCreate a virtual environment:
python -m venv venvInstall all required dependencies:
pip install -r requirements.txtActivate it:
source venv/bin/activateInstall all required modules (make sure you're is the same directory where the requirements.txt file is):
pip install -r requirements.txtRun the app with the following command (again make sure you're in the same directory as the app.py file):
flask runA quick look at the apps files and directories.
.
βββ cascades
| |ββ pretrained_001.xml
| |ββ ...
| βββ pretrained_NNN.xml
βββ data
| |ββ negatives
| βββ positives
βββ scripts
| βββ all scripts explained in section 4.
βββ testing
| βββ mostly testing images
βββ .gitignore
βββ Procfile
βββ README.md
βββ app.py
βββ requirements.txt
βββ runtime.txt
In this section an overview on how to use all the scripts to prepare data and train HAAR to detect the input object will be explained.
Fisrtly, we need to collect our data. Instead of shooitng the sky and waisting hundreds of hours on it, we'll use "laboratory" data which we'll fetch from an open-source software called Stellarium. A detailed approach on how to import and run scripts in Stellraium can be found here!
We have two scripts: one for fetching positive images (the ones containing the object we want to be able to detect) and one for fetching negative images (all other parts of the sky without that object), which can be found in scripts/
Before running this script, a few constants should be changed.
DESTINATION_PATHrepresents the system path where the images will be storedRA_TARGET_STARTstarting RA coordinate (in decimal degrees) for the object of interestRA_TARGET_ENDending RA coordinate (in decimal degrees) for the object of interestDEC_TARGET_STARTstarting DEC coordinate (in decimal degrees) for the object of interestDEC_TARGET_ENDending DEC coordinate (in decimal degrees) for the object of interest
After running this script you'll end up with a few (max. 30) images that contain the object of interest.
This script will take screen shoots of the whole sky, just change the following constant.
DESTINATION_PATHrepresents the system path where the images will be stored
After running this script you'll end up with more then 10k images of the sky.
The first step is to apply star masks onto the brightest stars in the positive images. To apply the star masks run the script scripts/StarDetector.py as follows:
py scripts/StarDetector.py --images PATH_TO_IMAGES_DIR --masksize MASK_SIZE_PERCENTAGE --outputname OUTPUT_NAME --percision PERCISION_PERCENTAGEThe parameters are:
PATH_TO_IMAGES_DIRrelative path to the directory containing the positive imagesMASK_SIZE_PERCENTAGEmask size percentage in realtion to the input image's width (e.g.0.06means6%Γ input image's width)OUTPUT_NAMEname that will be given to the output filesPERCISION_PERCENTAGEpercentage of the brightest star that will be used as the a thrashold value. To all the stars having an area size smaller then the threshold value won't be covered by the mask. Usage is similar toMASK_SIZE_PERCENTAGE(e.g.0.18means18%)
After successfully appyling masks onto stars crop the image so only the object of interest will be visible.
The next step is to resize all images to some desired dimensions (e.g. 500Γ500px).
Run the script as follows:
py scripts/Resizer.py --images PATH_TO_IMAGES_DIR --size SIZE --grayscale VALUEThe parameters are:
PATH_TO_IMAGES_DIRrelative path to the directory containing the cropped positive imagesSIZEsize in pixels (size Γ size) for the output imagesVALUEif images need to be converted to grayscale set0, if not set to any other number
This step will generate many new positive images (with distorsions) that will be used to train the cascade. The new images are generated by applying the exisitng positive images onto the negative images but with distorisions.
As this script uses the openCV library as a system module, please install openCV as a system module (perhaps the best is to run it on Linux as it is the easiest to install openCV as a system module there).
Run the script as follows:
python scripts/PrepareSamples.py --pos POSITIVES_DIR --neg NEGATIVES_DIR --num NUMBER_OF_NEW --maxxangle MAX_X_ANGLE --maxyangle MAX_Y_ANGLE --maxzangle MAX_Z_ANGLEThe parameters are:
POSITIVES_DIRrelative path to the directory containing the positive imagesNEGATIVES_DIRrelative path to the directory containing the negative imagesNUMBER_OF_NEWnumber of positives that will be genmerated for each existing positiveMAX_X_ANGLEthe max. angle on the x-axis the positives will be rotated while appyling them on the negatives (best opetion0.0)MAX_Y_ANGLEthe max. angle on the y-axis the positives will be rotated while appyling them on the negatives (best opetion0.0)MAX_Z_ANGLEthe max. angle on the z-axis the positives will be rotated while appyling them on the negatives (best opetion0.0)
After executing this script a directory named final_samples will be created containing all new positive images and the final_samples.txt file needed for the newx step. Also two files named positives.txt and negatives.txt will appear.
To generate the input vector for the HAAR training, run the following openCV command:
opencv_createsamples -info SAMPLES_LIST -num NUMBER -w WIDTH -h HEIGHT -vec VECOTR -maxxangle MAX_X_ANGLE -maxyangle MAX_Y_ANGLE -maxzangle MAX_Z_ANGLEThe parameters are:
SAMPLES_LISTlist file (.txt) with all positive samples (final_samples/final_samples.txtfrom last step)NUMBERnumber of positive images that will be used for the creation of the vector fileWIDTHwidth of the input images (last step used24)HEIGHTheight of the input images (last step used24)VECOTRname of the output vector file (e.g.positives.vec)MAX_X_ANGLEthe max. angle on the x-axis the positives have been rotatedMAX_Y_ANGLEthe max. angle on the y-axis the positives have been rotatedMAX_Z_ANGLEthe max. angle on the z-axis the positives have been rotated
After preparing all input files for the HAAR training process, hit the following command:
opencv_traincascade -data DATA -vec VECOTR -bg NEGATIVES_LIST -numPos NUMBER_POS -numNeg NUMBER_NEG -numStages NUMBER_STAGES -width WIDTH -height HEIGHT -mode ALL -bt DAB -minHitRate 0.995 -maxFalseAlarmRate 0.5 -maxWeakCount 100 -maxDepth 1 -precalcValBufSize 1024 -precalcIdxBufSize 1024The parameters are:
DATAdirectory with final samples from last stepsVECOTRvector file from last stepNEGATIVES_LISTnegatives list from last step (negatives.txt)NUMBER_POSnumber of positive images that will be used to train the cascadeNUMBER_NEGnumber of negative images that will be used to train the cascadeNUMBER_STAGESnumber of stages HAAR will take to train the cascade (min. 1, max. 20)WIDTHwidth of the input images (last step used24)HEIGHTheight of the input images (last step used24)
Other parameters should be kept as they are.
After the training finishes, an output file named cascade.xml will be created in the directory final_samples/.
The HAAR detection script can be run using the following command:
python scripts/HaarDetection.py --images IMAGES_DIR --masksizeMin MASK_SIZE_MIN --masksizeMax MASK_SIZE_MAX --outputname OUTPUT_NAME --percisionMin PERCISION_MIN --percisionMax PERCISION_MAX --cascade CASCADES_DIR --scale 1.01 --minNghb 2 --json JSON_FILE --plot 0 --streach 0The parameters are:
IMAGES_DIRpath to dircetory containing the input images on which you want to detect objectsMASK_SIZE_MINminimum mask size in percentage as described in 4.2.2.MASK_SIZE_MAXmaximum mask size in percentage as described in 4.2.2.OUTPUT_NAMEname of the output directory and filesPERCISION_MINminimum percision in percentage as described in 4.2.2. (preferably0.0)PERCISION_MAXmaximum percision in percentage as described in 4.2.2.CASCADES_DIRdirectory containing all the cascade files (the output files from step 4.3. should be placed into this directory and renamed to the desired name of the object question)JSON_FILEpath to the json file
Other parameters should be as they are. If detection is unsucessful change the streach parameter to 1, if you want to plot every image in every step of the detectio script change the plot parameter to 1.
"The real friends of the space voyager are the stars. Their friendly, familiar patterns are constant companions, unchanging, out there." - James Lovell, Apollo Astronaut
Python Script created by Marin Maslov @ FESB (UNIST)