Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Database Migration for sorting images: #352

Merged
merged 40 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
45c0952
Add Database Migration for sorting images:
jLeonBi May 31, 2021
2ede036
Add python scripts:
jLeonBi Jun 1, 2021
bf46a0c
Add empty jobs and associated test:
jLeonBi Jun 2, 2021
79a303c
Work on GenerateImageHash:
jLeonBi Jun 8, 2021
53b992f
Fill SimilarityIndex Job and script:
jLeonBi Jun 9, 2021
e6a1898
Change chack if hash is available
jLeonBi Jun 9, 2021
b7babf7
add install imagehash but Failed
jLeonBi Jun 15, 2021
daeb7dc
change install from imagehash to suggested one
jLeonBi Jun 15, 2021
c3a7edd
Fix worker dockerfile
mzur Jun 15, 2021
6d10209
rewrite GenerateHashValueJob:
jLeonBi Jun 15, 2021
820d235
Merge branch 'imageCompare' of https://github.com/jLeonBi/core into i…
jLeonBi Jun 15, 2021
9891b7e
rewrite generateHashValue:
jLeonBi Jun 16, 2021
f9db386
Change HashValueGenerator script:
jLeonBi Jun 16, 2021
19f6b86
work on generateHashValue job:
jLeonBi Jun 21, 2021
918f33c
Fix first time handle job:
jLeonBi Jun 21, 2021
5656448
update python scripts:
jLeonBi Jun 21, 2021
c5a955b
change output file form SamilarityIndexGenerator:
jLeonBi Jun 21, 2021
7d4aefa
add GerneatSimilarityIndex Job:
jLeonBi Jun 22, 2021
8e94659
update GenerateHashValue and GenerateSimilarityIndex:
jLeonBi Jun 24, 2021
973d06d
rewrite GenerateSimilartiyIndex:
jLeonBi Jun 24, 2021
65768e5
Add starting Jobs:
jLeonBi Jun 25, 2021
ceb76b4
apply suggested changes from mzur
jLeonBi Jun 25, 2021
2b69863
Merge branch 'master' of https://github.com/biigle/core into imageCom…
mzur Jun 28, 2021
e5fd021
Merge for starting docker and add boarder case:
jLeonBi Jun 29, 2021
1631983
Apply code readable suggestions:
jLeonBi Jun 30, 2021
af0714f
change npmrc for pushing
jLeonBi Jul 11, 2021
d871422
remove old token
jLeonBi Jul 11, 2021
2e5a4d0
add new token
jLeonBi Jul 11, 2021
85aa868
remove npmrc
jLeonBi Jul 11, 2021
2f6a01a
Delete .npmrc
jLeonBi Jul 11, 2021
48fa4ed
empty npmrc
jLeonBi Jul 11, 2021
69d71c4
remove old npmrc add new one
jLeonBi Jul 11, 2021
7da371d
Work on Frontend for Similarity Index Sorting:
jLeonBi Jul 12, 2021
3932686
remove unneccessary dd
jLeonBi Jul 12, 2021
d0a14f8
run npm run prod
jLeonBi Jul 12, 2021
bebb618
Work on similarityindex sorter:
jLeonBi Jul 14, 2021
87d1dc5
update sorting function for similarity index
jLeonBi Jul 17, 2021
b947b68
Add test for SimilarityIndexController:
jLeonBi Jul 17, 2021
90ce456
add crated for getting volume id
jLeonBi Jul 20, 2021
2e5c4d7
Finsish image compare project:
jLeonBi Jul 21, 2021
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
41 changes: 4 additions & 37 deletions app/Jobs/GenerateHashValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function handle()
{
$hashValue = $this->getHashValue($this->image);
if ($hashValue !== null) {
$this->image->hash = $hashValue["hash"];
$this->image->hash = $hashValue;
$this->image->save();
}
}
Expand Down Expand Up @@ -122,10 +122,8 @@ protected function getHashValue(Image $image)
$script = config('biigle.hash_value_generator');

try {
$outputPath = $this->getOutputJsonPath($image);
$inputPath = $this->createInputJson($image, $imageByteString);
$output = $this->python("{$script} {$inputPath} {$outputPath}");
$hashValue = $this->decodeOutputJson($outputPath);
$hashValue = $this->python("{$script} {$inputPath}")[0];
mzur marked this conversation as resolved.
Show resolved Hide resolved
} catch (Exception $e) {
$input = File::get($inputPath);
$hashValue = null;
Expand All @@ -134,10 +132,6 @@ protected function getHashValue(Image $image)
if (isset($inputPath)) {
$this->maybeDeleteFile($inputPath);
}

if (isset($outputPath)) {
$this->maybeDeleteFile($outputPath);
}
}
return $hashValue;
} else {
Expand Down Expand Up @@ -181,7 +175,7 @@ protected function python($command)
*/
protected function getInputJsonPath(Image $image)
{
return config('hash.tmp_dir')."/generate_hash_value_input_{$image->id}.json";
return config('biigle.tmp_dir')."/generate_hash_value_input_{$image->id}";
}

/**
Expand All @@ -195,38 +189,11 @@ protected function getInputJsonPath(Image $image)
protected function createInputJson(Image $image, $imageAsByteString)
{
$path = $this->getInputJsonPath($image);
$content = json_encode([
'image_as_byte_string' => base64_encode($imageAsByteString),
'image_id' => $image->id,
]);

File::put($path, $content);
File::put($path, $imageAsByteString);
return $path;
}

/**
* Get the path to to output file for the HashValueGenerator script.
*
* @param Image $image
*
* @return string
*/
protected function getOutputJsonPath(Image $image)
{
return config('hash.tmp_dir')."/generate_hash_value_output_{$image->id}.json";
}

/**
* @param path Opens the ouput path after the python script and decodes it
*
* @return mixed
*/
protected function decodeOutputJson($path)
{
return json_decode(File::get($path), true);

}


/**
* Delete a file if it exists.
Expand Down
19 changes: 4 additions & 15 deletions app/Jobs/GenerateSimilarityIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public function createSimilarityIndexArray(array $imageHashArray)
$outputPath = $this->getOutputJsonPath($this->volume);
$inputPath = $this->createInputJson($this->volume, $imageHashArray);
$output = $this->python("{$script} {$inputPath} {$outputPath}");
$similarityIndexArray = $this->decodeOutputJson($outputPath);
} catch (Exception $e) {
$similarityIndexArray = json_decode(File::get($outputPath), true);
} catch (Exception $e) {
$input = File::get($inputPath);
$similarityIndexArray = null;
throw new Exception("Input: {$input}\n" . $e->getMessage());
Expand Down Expand Up @@ -180,7 +180,7 @@ protected function python($command)
*/
protected function getInputJsonPath(Volume $volume)
{
return config('hash.tmp_dir')."/generate_sim_index_input_{$volume->id}.json";
return config('biigle.tmp_dir')."/generate_sim_index_input_{$volume->id}.json";
}

/**
Expand All @@ -192,7 +192,7 @@ protected function getInputJsonPath(Volume $volume)
*/
protected function getOutputJsonPath(Volume $volume)
{
return config('hash.tmp_dir')."/generate_sim_index_output_{$volume->id}.json";
return config('biigle.tmp_dir')."/generate_sim_index_output_{$volume->id}.json";
}

/**
Expand All @@ -211,17 +211,6 @@ protected function createInputJson(Volume $volume, Array $imagesHashArray)
return $path;
}

/**
* @param path Opens the ouput path after the python script and decodes it
*
* @return mixed
*/
protected function decodeOutputJson($path)
{
return json_decode(File::get($path), true);

}

/**
* Delete a file if it exists.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AddHashToImagesTable extends Migration
public function up()
{
Schema::table('images', function (Blueprint $table) {
$table->string('hash')->nullable();
$table->longText('hash')->nullable();
mzur marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down
15 changes: 2 additions & 13 deletions resources/scripts/HashValueGenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,10 @@ def createHashValue(img):
return createdHash


with open(sys.argv[1]) as inputJson:
image_data = json.load(inputJson)

id = image_data['image_id']
imageBytes = image_data['image_as_byte_string']

with Image.open(io.BytesIO(base64.b64decode(imageBytes))) as img:
with Image.open(sys.argv[1]) as img:
createdHash = createHashValue(img)


returnData = {
'id': id,
'hash': str(createdHash)
}

with open(sys.argv[2], 'w') as f:
json.dump(returnData, f)

print(str(createdHash))
33 changes: 8 additions & 25 deletions tests/php/Jobs/GenerateHashValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ public function setUp(): void
}
public function testHandle()
{
Storage::disk(config('thumbnails.storage_disk'))->put('test.jpg', 'test_data');

$image = ImageTest::create([
'filename' => 'test.jpg'
]);
$prefix = fragment_uuid_path($image->uuid);
$format = config('thumbnails.format');

Storage::disk(config('thumbnails.storage_disk'))->put("{$prefix}.{$format}", $image);


with(new GenerateHashValueStub($image))->handle();

Expand All @@ -38,37 +43,15 @@ public function testHashInitialization()
'filename' => 'test.jpg'
]);

assertNull($image->hash);
$this->assertNull($image->hash);
}
}

class GenerateHashValueStub extends GenerateHashValue
{
protected function python($command)
{
return "{'hash': 'jmhsdgfmasgh'}";
}


protected function getThumbnail(Image $image)
{
return b'\xe1\x88\xb4';
}



protected function decodeOutputJson($path) {
return [
"id" => 1,
"hash" => 'hopfullyAHashValue'
];
}



protected function createInputJson(Image $image, $imageAsByteString)
{
return 'abcd';
return array('024a60dfefffcfff02486dcfcffeffff010c07f1bffeffff0144907dbfffffff004018ffbbffffff00661c077ffbffff006c1e007ff7fbff8a021e679fffffef830307879707efff02c6c311af07ffff108303ebc9e9ff7f00800280c519ffff00100680c703feff018264e01217fff3018026d04100cfe3068193c0808007eb0007fb48000027bf0044df586031c7bf00093f01c003fb3f00083e226007fcd0001c78c13887e4d000143be0c265fe13000418fcf60dff1f00240bfe1e08ffe6000a0346320a6bdf000f90cff7c6e7bf000981ee334ff76f000100fff3dfffbf0137807fa9df3fbf0021813ff89e3fed0010070fbc0cbeef00020307bd0fceff00000047bf8fefff0000507f1f0fffff0000c0023c07fedf000180001c0ffd9f00000000003fffff00018000001efff3000200000666f7ff01c0000004faffefc0c0180000fbdfffc080000001ffe73f41000000496fe7330000020403ffff9f0000020c00dfffff09000006103fedff00002023833f63ff00000022c3ff479f10c8016efffff7df102200fa7ef7cbdf002200106209ee370000300d218fee1b0000060f191bf71f000001020a1ff33f000080e2248efa73000000430778f9f7000000010278e07f00012008d87fae6f00020009f99bfe6f00103418c884fb3b000010004e0ce73d0000003fd84007bf0000213dfe4e17ff00005307601cf4df');
mzur marked this conversation as resolved.
Show resolved Hide resolved
}

}
Expand Down
25 changes: 8 additions & 17 deletions tests/php/Jobs/GenerateSimilarityIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testHandle()
public function testSimilarityIndexInitialization()
{
$volume = VolumeTest::create();
$i1 = ImageTest::create(['filename' => 'test.jpg', 'volume_id' => $volume->id, 'hash' => 'dqhdkjhqlwdlhlbsbjkk']);
$i1 = ImageTest::create(['filename' => 'test.jpg', 'volume_id' => $volume->id, 'hash' => '024a60dfefffcfff02486dcfcffeffff010c07f1bffeffff0144907dbfffffff004018ffbbffffff00661c077ffbffff006c1e007ff7fbff8a021e679fffffef830307879707efff02c6c311af07ffff108303ebc9e9ff7f00800280c519ffff00100680c703feff018264e01217fff3018026d04100cfe3068193c0808007eb0007fb48000027bf0044df586031c7bf00093f01c003fb3f00083e226007fcd0001c78c13887e4d000143be0c265fe13000418fcf60dff1f00240bfe1e08ffe6000a0346320a6bdf000f90cff7c6e7bf000981ee334ff76f000100fff3dfffbf0137807fa9df3fbf0021813ff89e3fed0010070fbc0cbeef00020307bd0fceff00000047bf8fefff0000507f1f0fffff0000c0023c07fedf000180001c0ffd9f00000000003fffff00018000001efff3000200000666f7ff01c0000004faffefc0c0180000fbdfffc080000001ffe73f41000000496fe7330000020403ffff9f0000020c00dfffff09000006103fedff00002023833f63ff00000022c3ff479f10c8016efffff7df102200fa7ef7cbdf002200106209ee370000300d218fee1b0000060f191bf71f000001020a1ff33f000080e2248efa73000000430778f9f7000000010278e07f00012008d87fae6f00020009f99bfe6f00103418c884fb3b000010004e0ce73d0000003fd84007bf0000213dfe4e17ff00005307601cf4df']);
$i2 = ImageTest::create(['volume_id' => $volume->id, 'filename' => 'a.jpg', 'hash' => 'o8793r2hriuh9e2i3bd9']);
$i3 = ImageTest::create(['volume_id' => $volume->id, 'filename' => 'b.jpg', 'hash' => 'dqhdkjhqlwdlhlbsbjkk']);
$i4 = ImageTest::create(['volume_id' => $volume->id, 'filename' => 'c.jpg', 'hash' => 'kjsbfabefhlbeqwhkcbd']);
Expand Down Expand Up @@ -75,21 +75,12 @@ class GenerateSimilarityIndexStub extends GenerateSimilarityIndex

protected function python($path)
{
return "{2: 0, 5: 1, 4: 2, 1: 3, 3: 4}";
}

protected function createInputJson(Volume $volume, Array $imagesHashArray)
{
return 'abcd';
$i = 10;
$imageArray = $this->volume->images()->pluck("hash", "id");
foreach ($imageArray as $id => $hash) {
$imageArray[$id] = $i % 5;
$i++;
}
return File::put($this->getOutputJsonPath($this->volume), $imageArray);
}

protected function decodeOutputJson($path) {
return [
2 => 0,
5 => 1,
4 => 2,
1 => 3,
3 => 4
];
}
}