2

enter image description here

I am getting ValueError because it is unable to convert string to float please help to remove this error on lines

x0 = float(st[0:comma])
u = float(st[comma+1:]) 

my rsain.txt file contains:

3
7 
11
4
8

This is my code:

import logistic
import histogram
import cv2
import numpy as np
import rsa
import pywt
path=input("Enter the path of the original image: ")
x0=input("Enter the value of X(0) between 0 and 1: ")
u=input("Enter the value of u between 3.5699456 and 4: ")

#sending scrambling parameters for RSA
s=str(x0)+','+str(u)
rsa.encrypt(s)

img=cv2.imread(path,0)
histogram.get_histogram(path,'histogram/watermark.png')
h,w = np.shape(img)

#generating array of pseudo-random numbers using CLM
x=logistic.get_x_array(x0,u,h)

#scrambling
c=0
for py in range(0,h):
    for px in range(0,w):
        img[py][px]=(img[py][px]^x[c])%256
        c=c+1
cv2.imwrite('images/scrambled_watermark.jpg',img)
histogram.get_histogram('images/scrambled_watermark.jpg','histogram/scrambled_watermark.jpg')
#DWT
hpath=input("Enter the path of the host image: ")
img = cv2.imread(hpath,0)
histogram.get_histogram(hpath,'histogram/host_image_watermark.jpg')
#Finding components
Coefficients = pywt.wavedec2(img, wavelet='haar', level=1)
shape_LL = Coefficients[0].shape #Coefficients[0] is LL

#SVD
Uc, Sc, Vc = np.linalg.svd(Coefficients[0])
W= cv2.imread('images/scrambled_watermark.jpg',0)

#converting 1-d Sc to diagonal matrix
SLL = np.zeros(shape_LL)
row = min(shape_LL)
SLL[:row, :row] = np.diag(Sc)
Sc=SLL

#adding watermark
alpha=0.1
Snew=np.zeros((min(shape_LL),min(shape_LL)))
for py in range(0,min(shape_LL)):
    for px in range(0,min(shape_LL)):
        Snew[py][px]=Sc[py][px]+alpha*(W[py][px])
        
#SVD again
Uw, Sw, Vw = np.linalg.svd(Snew)

LLnew=np.zeros((min(SLL.shape),min(SLL.shape)))
LLnew=Uc.dot(np.diag(Sw)).dot(Vc)

Coefficients[0]=LLnew
i=pywt.waverec2(Coefficients, 'haar')
cv2.imwrite('images/watermarked_image.jpg',i)
histogram.get_histogram('images/watermarked_image.jpg','histogram/watermarked_image.jpg')

###################extraction#######################

xyz=input("Press Enter to start extraction")
path=input("Enter the path of original host image: ")
oimg=cv2.imread(path,0)
path2="images/watermarked_image.jpg"
wimg=cv2.imread(path2,0)


#DWT
C = pywt.wavedec2(oimg, wavelet='haar', level=1)
shape_LL = C[0].shape #C[0] is LL
Cw= pywt.wavedec2(wimg, wavelet='haar', level=1)

#SVD
Ucw, Scw, Vcw = np.linalg.svd(Cw[0])
Uc, Sc, Vc = np.linalg.svd(C[0])

Uw, Sw, Vw = np.linalg.svd(Snew)
LLnew1=Uw.dot(np.diag(Scw)).dot(Vw)


Wdnew=np.zeros((min(shape_LL),min(shape_LL)))

Scdiag = np.zeros(shape_LL)
row = min(shape_LL)
Scdiag[:row, :row] = np.diag(Sc)
Sc=Scdiag


alpha=0.1
for py in range(0,min(shape_LL)):
    for px in range(0,min(shape_LL)):
        Wdnew[py][px]=(LLnew1[py][px]-Sc[py][px])/alpha

cv2.imwrite('images/recovered_watermark.jpg',Wdnew)

with open('rsain.txt') as f:
    content = f.readlines()
content = [int(x.strip()) for x in content]
st=rsa.decrypt(content)
comma=st.find(',')
x0 = float(st[0:comma])
u = float(st[comma+1:])

#Unscrambling
x=logistic.get_x_array(x0,u,min(shape_LL)) #generating array of pseudo-random numbers using CLM
img=cv2.imread('images/recovered_watermark.jpg',0)
h,w = np.shape(img)
c=0
for py in range(0,h):
    
    for px in range(0,w):
        img[py][px]=(img[py][px]^x[c])%256
        c=c+1
cv2.imwrite('images/unscrambled_watermark.jpg',img)
histogram.get_histogram('images/unscrambled_watermark.jpg','histogram/unscrambled_watermark.jpg')

Its logistic.py file

def get_x_array(x0, u, h):
    x = []
    a=100
    b=100
    c=x0

    while len(x)<(h*h):
        c = u * c * (1.0 - c)
        x.append(c)
    for i in range(0,h*h):
        if(round(b*x[i])<(b*x[i])):
            x[i]=int((round(a*(b*x[i]-round(b*x[i]))))%256)
        if(round(b*x[i])>(b*x[i])):
            x[i]=int((round(a*(1-b*x[i]-round(b*x[i]))))%256)
    return x


Ragini
  • 33
  • 5
  • Can you add a `print(st)` on the line after you decrypt it. The problem might be that all the numbers are in that string so it can’t turn into a float. Edit your answer with what the value of `st` is afterwards. – Eno Gerguri Jun 26 '20 at 08:22
  • i tried but i am getting the error that I have displayed above –  Ragini Jun 26 '20 at 08:44
  • The decrypted message looks like symbols. Are the numbers in rsa? And if so you need to ensure that you put in the second argument of `rsa.decrypt(content, privateKey)` if you are already using the second argument and just do not want to show your key at least put the variable name there so we can understand that you have used it. – Eno Gerguri Jun 26 '20 at 08:54
  • yes u can check now i dont know how to change string to float when i tried to remove float i got another error in logistic.py file plz help –  Ragini Jun 26 '20 at 12:32
  • what is the error when you remove `float()` . The problem is not the `float` on a string but instead the problem is probably something to do with the way you are encrypting and decryption using rsa. I'm not sure if this helps but a stack overflow questions was asked on how to encrypt using RSA and how to decrypt: https://stackoverflow.com/questions/30056762/rsa-encryption-and-decryption-in-python Other than that I will put a bounty on this question when it becomes possible to do so! – Eno Gerguri Jun 26 '20 at 13:02
  • if we correct this error that wont come its interconnected if I don't change this to float then the error comes –  Ragini Jun 26 '20 at 15:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216756/discussion-between-ragini-and-eno-gerguri). –  Ragini Jun 27 '20 at 05:49
  • hey i will send you the whole project can u help me to decode the error?if yes plz send e your email id thank you –  Ragini Nov 29 '20 at 07:07

0 Answers0