From 7687fc283c48b97fc1dff8c733b2c0d6cb016554 Mon Sep 17 00:00:00 2001 From: westernmeadow Date: Tue, 2 Jan 2024 23:13:45 -0800 Subject: [PATCH] fixes to image tables --- Forums/DB_Connection/db_connection.py | 46 ++++++++++----------- Forums/Utilities/utilities.py | 3 ++ MarketPlaces/DB_Connection/db_connection.py | 36 ++++++++-------- MarketPlaces/Utilities/utilities.py | 3 ++ 4 files changed, 47 insertions(+), 41 deletions(-) diff --git a/Forums/DB_Connection/db_connection.py b/Forums/DB_Connection/db_connection.py index 1fc9d6a..1409082 100644 --- a/Forums/DB_Connection/db_connection.py +++ b/Forums/DB_Connection/db_connection.py @@ -118,9 +118,9 @@ def verifyImage(cur, base64Image): try: - cur.execute("lock table market_images IN ACCESS EXCLUSIVE MODE") + cur.execute("lock table forum_images IN ACCESS EXCLUSIVE MODE") - cur.execute("select image_id from market_images where hash_image = %(hashImage)s limit 1", + cur.execute("select image_id from forum_images where hash_image = %(hashImage)s limit 1", {'hashImage': generate_image_hash(base64Image)}) recset = cur.fetchall() @@ -273,7 +273,7 @@ def getLastImage(cur): try: - cur.execute("select image_id from market_images order by image_id desc limit 1") + cur.execute("select image_id from forum_images order by image_id desc limit 1") recset = cur.fetchall() @@ -406,7 +406,7 @@ def create_author(cur, row, forumId): "signature_user, image_user, dateinserted_user) Values (%s, %s, %s, %s, %s, %s, %s, %s, %s)" recset = [userId, forumId, - row[2], 'Nan', 'Nan', 'Nan', 'Nan', 'Nan', #telling the create_user function to not track changes here + row[2], 'Nan', 'Nan', 'Nan', 'Nan', None, #telling the create_user function to not track changes here row[8]] cur.execute(sql, recset) @@ -451,7 +451,7 @@ def create_user(cur, row, forumId, index): recset = cur.fetchall() imageId = recset[0]['image_user'] - if not is_same_image(row[9][index], recset[0]['image_user'])): + if not is_same_image(cur, row[9][index], recset[0]['image_user']): imageId = create_image(cur, row[9][index], row[8]) if (str(recset[0]['status_user']) != str(row[11][index] if row[11][index] != '-1' else None) or @@ -464,7 +464,7 @@ def create_user(cur, row, forumId, index): str(recset[0]['reputation_user']) != 'Nan' or str(recset[0]['interest_user']) != 'Nan' or str(recset[0]['signature_user']) != 'Nan' or - str(recset[0]['image_user']) != 'Nan'): + str(recset[0]['image_user']) != str(None)): userVersionId = int(getLastUserVersion(cur, userId) + 1) @@ -537,7 +537,7 @@ def create_posts(cur, row, forumId, topicId): recset = cur.fetchall() imageId = recset[0]['image_post'] - if not is_same_image(row[18][i], recset[0]['image_post'])): + if not is_same_image(cur, row[18][i], recset[0]['image_post']): imageId = create_image(cur, row[18][i], row[8]) if (str(recset[0]['content_post']) != str(row[15][i]) or @@ -566,23 +566,23 @@ def create_posts(cur, row, forumId, topicId): 'dateinserted_post': row[8], 'postId': postId}) -def is_same_image(newBase64Image, oldImageId): +def is_same_image(cur, newBase64Image, oldImageId): - if newBase64Image == "-1" and oldImageId == "-1": + if newBase64Image == "-1" and str(oldImageId) is str(None): return True - if newBase64Image == "-1": # and oldImageId != "-1" + if newBase64Image == "-1": # and str(oldImageId) != str(None) return False - if oldImageId == "-1": # and newBase64Image != "-1" + if str(oldImageId) == str(None): # and newBase64Image != "-1" return False - sql = "select * from market_images where image_id = %(imageId)s" + sql = "select * from forum_images where image_id = %(imageId)s" cur.execute(sql, {'imageId': oldImageId}) recset = cur.fetchall() - decImage = decode_decrypt_image_in_base64(base64Image) + decImage = decode_decrypt_image_in_base64(newBase64Image) hash1 = generate_image_hash(decImage) hash2 = recset[0]['hash_image'] @@ -590,19 +590,21 @@ def is_same_image(newBase64Image, oldImageId): if hash1 == hash2: return True - features1 = json.loads(extract_hidden_layer_output(dec_string)) + features1 = json.loads(extract_hidden_layer_output(decImage)) features2 = json.loads(recset[0]['resnet50_image']) similarity = 1 - distance.cosine(features1, features2) - return similarity < 0.8 + return similarity >= 0.8 def create_image(cur, base64Image, date): if base64Image == "-1": return None - imageId = verifyImage(base64Image) + decImage = decode_decrypt_image_in_base64(base64Image) + + imageId = verifyImage(cur, decImage) if not imageId: imageId = int(getLastImage(cur) + 1) @@ -611,15 +613,13 @@ def create_image(cur, base64Image, date): newImage = False if newImage: - - decImage = decode_decrypt_image_in_base64(base64Image) hashImage = generate_image_hash(decImage) hloFeatures = extract_hidden_layer_output(decImage) keypoints, descriptors = extract_keypoints(decImage) - sql = "Insert into market_images(image_id, hash_image, base64_image, " \ - "resnet50_image, siftkey_image, siftdesc_image, dateinserted_image) " \ + sql = "Insert into forum_images(image_id, hash_image, base64_image, " \ + "resnet50_image, siftkey_image, siftdesc_image, dateinserted_image) " \ "Values (%s, %s, %s, %s, %s, %s, %s)" recset = [imageId, @@ -662,11 +662,11 @@ def create_database(cur, con): sql = "create table forum_images(image_id integer not null, hash_image character varying(64) not null, base64_image character varying(10000000) not null, " \ "resnet50_image character varying(1000000) null, siftkey_image character varying(1000000) null, siftdesc_image character varying(1000000) null, " \ - "dateinserted_item timestamp(6) with time zone not null, " \ - "constraint items_pk primary key (image_id))" + "dateinserted_image timestamp(6) with time zone not null, " \ + "constraint forum_images_pk primary key (image_id))" cur.execute(sql) - sql = "create unique index unique_image ON forum_images USING btree (hash_image ASC NULLS LAST)" + sql = "create unique index unique_forum_image ON forum_images USING btree (hash_image ASC NULLS LAST)" cur.execute(sql) sql = "create table forums (forum_id integer NOT NULL, name_forum character varying(255) NOT NULL, url_forum " \ diff --git a/Forums/Utilities/utilities.py b/Forums/Utilities/utilities.py index 2b13602..b9e7cbc 100644 --- a/Forums/Utilities/utilities.py +++ b/Forums/Utilities/utilities.py @@ -89,6 +89,9 @@ def extract_keypoints(image_string): keypoints, descriptors = sift.detectAndCompute(img, None) + if len(keypoints) == 0: + return None, None + return json.dumps(wrap_keypoints(keypoints)), json.dumps(descriptors.tolist()) diff --git a/MarketPlaces/DB_Connection/db_connection.py b/MarketPlaces/DB_Connection/db_connection.py index d601f8d..7dd9028 100644 --- a/MarketPlaces/DB_Connection/db_connection.py +++ b/MarketPlaces/DB_Connection/db_connection.py @@ -273,7 +273,7 @@ def create_vendor(cur, row, marketId): recset = cur.fetchall() imageId = recset[0]['image_vendor'] - if not is_same_image(row[21], recset[0]['image_vendor'])): + if not is_same_image(cur, row[21], recset[0]['image_vendor']): imageId = create_image(cur, row[21], row[23]) if (str(recset[0]['rating_vendor']) != str(row[2] if row[2] != '-1' else None) or # there was a change in the vendor information @@ -362,7 +362,7 @@ def create_items(cur, row, marketId, vendorId): recset = cur.fetchall() imageId = recset[0]['image_item'] - if not is_same_image(row[20], recset[0]['image_item'])): + if not is_same_image(cur, row[20], recset[0]['image_item']): imageId = create_image(cur, row[20], row[23]) if (str(recset[0]['vendor_id']) != str(vendorId) or @@ -389,7 +389,7 @@ def create_items(cur, row, marketId, vendorId): sql = "Insert into items_history (item_id, version_item, market_id, vendor_id, name_item, description_item, cve_item, ms_item, category_item, " \ "views_item, reviews_item, rating_item, dateadded_item, btc_item, usd_item, euro_item, quantitysold_item, " \ - "quantityleft_item, shippedfrom_item, shippedto_item, lastseen_item, image_item, href_item, dateinserted_item, " \ + "quantityleft_item, shippedfrom_item, shippedto_item, image_item, href_item, lastseen_item, dateinserted_item, " \ "classification_item) Values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, " \ "%s, %s, %s, %s, %s, %s)" @@ -425,7 +425,7 @@ def create_items(cur, row, marketId, vendorId): "rating_item = %(rating_item)s, dateadded_item = %(dateadded_item)s, btc_item = %(btc_item)s, " \ "usd_item = %(usd_item)s, euro_item = %(euro_item)s, quantitysold_item = %(quantitysold_item)s, " \ "quantityleft_item = %(quantityleft_item)s, shippedfrom_item = %(shippedfrom_item)s, shippedto_item = %(shippedto_item)s, " \ - "image_item = %(image_item)s, lastseen_item = %(lastseen_item)s, dateinserted_item = %(dateinserted_item)s." \ + "image_item = %(image_item)s, lastseen_item = %(lastseen_item)s, dateinserted_item = %(dateinserted_item)s, " \ "classification_item = %(classification_item)s where item_id = %(itemId)s" cur.execute(sql, {'vendor_id': vendorId, @@ -460,15 +460,15 @@ def create_items(cur, row, marketId, vendorId): return itemId -def is_same_image(newBase64Image, oldImageId): +def is_same_image(cur, newBase64Image, oldImageId): - if newBase64Image == "-1" and oldImageId == "-1": + if newBase64Image == "-1" and str(oldImageId) == str(None): return True - if newBase64Image == "-1": # and oldImageId != "-1" + if newBase64Image == "-1": # and str(oldImageId) != str(None) return False - if oldImageId == "-1": # and newBase64Image != "-1" + if str(oldImageId) == str(None): # and newBase64Image != "-1" return False sql = "select * from market_images where image_id = %(imageId)s" @@ -476,7 +476,7 @@ def is_same_image(newBase64Image, oldImageId): recset = cur.fetchall() - decImage = decode_decrypt_image_in_base64(base64Image) + decImage = decode_decrypt_image_in_base64(newBase64Image) hash1 = generate_image_hash(decImage) hash2 = recset[0]['hash_image'] @@ -484,19 +484,21 @@ def is_same_image(newBase64Image, oldImageId): if hash1 == hash2: return True - features1 = json.loads(extract_hidden_layer_output(dec_string)) + features1 = json.loads(extract_hidden_layer_output(decImage)) features2 = json.loads(recset[0]['resnet50_image']) similarity = 1 - distance.cosine(features1, features2) - return similarity < 0.8 + return similarity >= 0.8 def create_image(cur, base64Image, date): if base64Image == "-1": return None - imageId = verifyImage(base64Image) + decImage = decode_decrypt_image_in_base64(base64Image) + + imageId = verifyImage(cur, decImage) if not imageId: imageId = int(getLastImage(cur) + 1) @@ -505,15 +507,13 @@ def create_image(cur, base64Image, date): newImage = False if newImage: - - decImage = decode_decrypt_image_in_base64(base64Image) hashImage = generate_image_hash(decImage) hloFeatures = extract_hidden_layer_output(decImage) keypoints, descriptors = extract_keypoints(decImage) sql = "Insert into market_images(image_id, hash_image, base64_image, " \ - "resnet50_image, siftkey_image, siftdesc_image, dateinserted_image) " \ + "resnet50_image, siftkey_image, siftdesc_image, dateinserted_image) " \ "Values (%s, %s, %s, %s, %s, %s, %s)" recset = [imageId, @@ -556,11 +556,11 @@ def create_database(cur, con): sql = "create table market_images(image_id integer not null, hash_image character varying(64) not null, base64_image character varying(10000000) not null, " \ "resnet50_image character varying(1000000) null, siftkey_image character varying(1000000) null, siftdesc_image character varying(1000000) null, " \ - "dateinserted_item timestamp(6) with time zone not null, " \ - "constraint items_pk primary key (image_id))" + "dateinserted_image timestamp(6) with time zone not null, " \ + "constraint market_images_pk primary key (image_id))" cur.execute(sql) - sql = "create unique index unique_image ON market_images USING btree (hash_image ASC NULLS LAST)" + sql = "create unique index unique_market_image ON market_images USING btree (hash_image ASC NULLS LAST)" cur.execute(sql) sql = "create table marketplaces(market_id integer not null, name_market character varying(255) not null, " \ diff --git a/MarketPlaces/Utilities/utilities.py b/MarketPlaces/Utilities/utilities.py index c698436..d4e1595 100644 --- a/MarketPlaces/Utilities/utilities.py +++ b/MarketPlaces/Utilities/utilities.py @@ -89,6 +89,9 @@ def extract_keypoints(image_string): keypoints, descriptors = sift.detectAndCompute(img, None) + if len(keypoints) == 0: + return None, None + return json.dumps(wrap_keypoints(keypoints)), json.dumps(descriptors.tolist())