Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 206 additions & 0 deletions Untitled.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"#!{sys.executable} -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
"#!{sys.executable} -m pip install --upgrade pip"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#!{sys.executable} -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple\n",
"#!{sys.executable} -m pip install -r requirements.txt "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# run the following for cv2\n",
"#!sudo apt-get install libglu1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/python\n",
"import cv2 as cv\n",
"import numpy as np\n",
"import sys\n",
"import glob\n",
"import multiprocessing\n",
"import roi"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mCanceled future for execute_request message before replies were done"
]
},
{
"ename": "",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[1;31mThe Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
]
}
],
"source": [
"#TODO: Integrate with Henry's Code\n",
"#Get score\n",
"#Work on getting scale down\n",
"MIN_MATCH_COUNT = 4\n",
"DATAPATH = \"./data/standard/*\"\n",
"FILE = \"./data/demo-images/scattered4.jpg\"\n",
"\n",
"\n",
"def findInlier(img):\n",
"\t# Initiate SIFT detector\n",
"\n",
"\timg1 = img[0]\t\t# Image in question\n",
"\timg2 = img[1][0] \t# Gold standard image\n",
"\tsift = cv.SIFT()\n",
"\n",
"\tkp1, des1 = sift.detectAndCompute(img1, None)\n",
"\tkp2, des2 = sift.detectAndCompute(img2, None)\n",
"\n",
"\tFLANN_INDEX_KDTREE = 0\n",
"\tindex_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)\n",
"\tsearch_params = dict(checks=50)\n",
"\n",
"\tflann = cv.FlannBasedMatcher(index_params, search_params)\n",
"\n",
"\tmatches = flann.knnMatch(des1, des2, k=2)\n",
"\n",
"\t# Store all good matches as per Lowe's ratio test.\n",
"\tgood = [m for m, n in matches if m.distance < 0.7*n.distance]\n",
"\n",
"\tif len(good) > MIN_MATCH_COUNT:\n",
"\t\tsrc_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)\n",
"\t\tdst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)\n",
"\n",
"\t\tM, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC, 5.0)\n",
"\t\tmatchesMask = mask.ravel().tolist()\n",
"\n",
"\telse:\n",
"\t\t#print \"Not enough matches are found - %d/%d\" % (len(good), MIN_MATCH_COUNT)\n",
"\t\tmatchesMask = []\n",
"\n",
"\t# Print Inliers\n",
"\treturn (len(matchesMask), img[1][1])\n",
"\n",
"\n",
"#Gets the nearest neighbor for each tile in\n",
"#a list of ROI tiles to the testData\n",
"def NN(tiles, testData):\n",
"\t\"\"\"param tiles: The regions of interest, in the form of images.\n",
"\tparam testData: Array of 30 or forty images.\n",
"\t\"\"\"\n",
"\t#store the max inliers for each tile\n",
"\tmaxInliers = []\n",
"\tfor tile in tiles:\n",
"\n",
"\t\tpairs = [(tile.copy(), img2) for img2 in testData]\n",
"\t\tif False:\n",
"\t\t\tp = multiprocessing.Pool(processes = 4)\n",
"\t\t\tinliers = p.map(findInlier, pairs)\n",
"\t\telse:\n",
"\t\t\tinliers = [findInlier(pair) for pair in pairs]\n",
"\n",
"\t\ttheMax = max(inliers)\n",
"\n",
"\t\t#Show the tile to identify\n",
"\t\tcv.imshow(\"Tile to identify\", tile)\n",
"\t\tcv.waitKey(1)\n",
"\n",
"\t\t#Show the best guess tile\n",
"\t\tguess = cv.imread(theMax[1])\n",
"\t\tguess = cv.resize(guess, (0,0), fx = 0.2, fy = 0.2)\n",
"\t\tcv.imshow(\"The guess\", guess)\n",
"\t\tcv.waitKey(0)\n",
"\n",
"\t\tmaxInliers.append(theMax)\n",
"\n",
"\n",
"#Converts a list of image names into a list of\n",
"#tuple's where the first value of the tuple is \n",
"#the OpenCV image and the second value of the \n",
"#tuple is the file name\n",
"def getImage(files):\n",
"\treturn [(cv.resize(cv.imread(f),(0,0), fx = 0.2, fy = 0.2), f) for f in files]\n",
"\n",
"def main():\n",
"\t#Get images\n",
"\n",
"\t#get list of images in golden data set\n",
"\t# Obtains the array of images to the golden standards\n",
"\ttestData = glob.glob(DATAPATH)\n",
"\timages = getImage(testData)\n",
"\t\n",
"\t#REMOVE ONCE HENRY'S DONE\n",
"\t# tiles contains an array of images\n",
"\ttiles = roi.findRoi(FILE)\n",
"\tinliers = NN(tiles, images)\n",
"\n",
"if __name__ == \"__main__\":\n",
"\tmain()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
},
"vscode": {
"interpreter": {
"hash": "3ad933181bd8a04b432d3370b9dc3b0662ad032c4dfaa4e4f1596c548f763858"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
4 changes: 2 additions & 2 deletions box.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ def create(cls, boxCon):

def __init__(self, x, y, width, height):
if width < 0 or height < 0:
print Fore.RED + "WARNING: Width or height is less than 0." + Fore.RESET
print( Fore.RED + "WARNING: Width or height is less than 0." + Fore.RESET )
elif x < 0 or height < 0:
print Fore.RED + "WARNING: x or y is less than 0." + Fore.RESET
print( Fore.RED + "WARNING: x or y is less than 0." + Fore.RESET )

self.startX = x
self.startY = y
Expand Down
18 changes: 13 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
Pillow==2.4.0
numpy==1.8.1
scipy==0.13.3
wsgiref==0.1.2
colorama==0.3.1
# Pillow==2.4.0
Pillow
#numpy==1.8.1
numpy
#scipy==0.13.3
scipy
#wsgiref==0.1.2
wsgiref
# colorama==0.3.1
colorama
#opencv==3.1
#opencv-python==2.4.9
opencv-python
Loading