Giter Site home page Giter Site logo

rahmatnazali / pimage Goto Github PK

View Code? Open in Web Editor NEW
159.0 5.0 56.0 575.4 MB

Python package for detecting copy-move attack on a digital image

Home Page: https://pypi.org/project/pimage/

License: Apache License 2.0

Python 100.00%
python copy-paste copy-move forensics image-processing attack fraud image

pimage's Introduction

This is a python package for detecting copy-move attack on a digital image.

This project is part of our paper that has been published at Springer. More detailed theories and steps are explained there.

Using the package

To install the package, simply hit it with pip: pip3 install pimage. Example script for using this package is also provided here.

Configuring the algorithm

The algorithm can be dynamically configured with Configuration class. If omitted, the default value from both of the paper will be used. The default value and description for each of the parameter is detailed on configuration.py.

from pimage.configuration import Configuration

conf = Configuration(
    block_size=32,
    nn=2,
    nf=188,
    nd=50,
    p=(1.80, 1.80, 1.80, 0.0125, 0.0125, 0.0125, 0.0125),
    t1=2.80,
    t2=0.02
)
  • Determining the block_size: The first algorithm use block size of 32 pixels so this package will use the same value by default. Increasing the size means faster run time at a reduced accuracy. Analogically, decreasing the size means longer run time with increased accuracy.

API for the detection process

  • The API for detection process is provided via copy_move.detect() method. For example:

    from pimage import copy_move
    from pimage.configuration import Configuration
    
    conf = Configuration(block_size=32)
    
    fraud_list, ground_truth_image, result_image = copy_move.detect("dataset_example_blur.png", configuration=conf)
  • fraud_list will be the list of (x_coordinate, y_coordinate) of the blocks group and the total number of the blocks it is formed with. If this list is not empty, we can assume that the image is being tampered. For example, running the cattle dataset with 32 px of block size will result in:

    ((-57, -123), 2178)
    ((-11, 140), 2178)
    ((-280, 114), 2178)
    ((-34, -305), 2178)
    ((-37, 148), 2178)
    

    the above output means there are 5 possible matched/identical region with 2178 overlapping blocks on each of it

  • ground_truth_image contains the black and white ground truth of the detection result. This is useful for comparing accuracy, MSE, etc with the ground truth from the dataset

  • result_image is the given image where the possible fraud region will be color-bordered (if any)

ground_truth_image and result_image will be formatted as numpy.ndarray. It can further be processed as needed. For example, it can be programmatically modified and then exported later as image like so:

import imageio

imageio.imwrite("result_image.png", result_image)
imageio.imwrite("ground_truth_image.png", ground_truth_image)

Quick command to detect an image

To quickly run the detection command for your image, the copy_move.detect_and_export() is also provided. The command is identical with .detect() but it also save the result to desired output path.

from pimage import copy_move

copy_move.detect_and_export('dataset_example_blur.png', 'output')

this code will save the ground_truth_image and result_image inside output folder.

Verbose mode

When running copy_move.detect() or copy_move.detect_and_export(), you can pass verbose=True to output the status of each step. The default value will be False so nothing will be printed.

Example output when verbose mode is being enabled:

Processing: dataset/multi_paste/cattle_gcs500_copy_rb5.png
Step 1 of 4: Object and variable initialization
Step 2 of 4: Computing characteristic features
100%|██████████| 609/609 [04:14<00:00,  2.39it/s]
Step 3 of 4:Pairing image blocks
100%|██████████| 241163/241163 [00:00<00:00, 816659.95it/s]
Step 4 of 4: Image reconstruction
Found pair(s) of possible fraud attack:
((-57, -123), 2178)
((-11, 140), 2178)
((-280, 114), 2178)
((-34, -305), 2178)
((-37, 148), 2178)
Computing time : 254.81 second
Sorting time   : 0.89 second
Analyzing time : 0.3 second
Image creation : 1.4 second
Total time    : 0:04:17 second 

The algorithm

The implementation generally manipulates overlapping blocks, and are constructed based on two algorithms:

  1. Duplication detection algorithm, taken from Exposing Digital Forgeries by Detecting Duplicated Image Region (alternative link); Fast and smooth attack detection algorithm on digital image using principal component analysis, but sensitive to noise and any following manipulations that are being applied after the attack phase (in which they call it post region duplication process)
  2. Robust detection algorithm, taken from Robust Detection of Region-Duplication Forgery in Digital Image; Relatively slower process with rough result on the detection edge but are considered robust towards noise and post region duplication process

How do we modify them?

We know that the first algorithm use coordinate and principal_component features, while the second algorithm use coordinate and seven_features.

Knowing that, we then attempt to give a tolerance by merging all the features like so:

Modification diagram

The attributes are saved as one object. A lexicographical sorting is then applied to the principal component and the seven features.

The principal component will bring similar block closer, while the seven features will back up the detection for a block that can't be detected by principal component due to being applied with post region duplication process (for example being blurred).

By doing so, the new algorithm will have a tolerance regarding variety of the input image. The detection result will be relatively smooth and accurate for any type of image, with a trade-off in run time as we basically run two algorithm.

Example image

All the result of the dataset should be inside output directory of this repository.

The image shown is ordered as: original, attacked, and the resulting detection image.

Horse dataset

Original image Forgered image Result image

Cattle dataset

Original image Forgered image Result image

Clean walls dataset

Original image Forgered image Result image

Knight moves dataset

Original image Forgered image Result image

Additional note

The project is formerly written with Python 2 for our Undergraduate Thesis, which is now left unmaintained here. The original thesis is written in Indonesian that in any case can also be downloaded from here.

pimage's People

Contributors

dependabot[bot] avatar likuilin avatar rahmatnazali avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pimage's Issues

help about output

hi,I used your example diagram for testing, but I copied and pasted it myself, again with blank results.
The way I do it is just regular copy and paste.
I wonder why?
Thank you very much!

Should have unit testing

The project should have unit testing. Currently the testing is only held by manually running the example script.

"resource/empty.png" is missing

When I run main_GUI.py I get an error:

Traceback (most recent call last):
  File "main_GUI.py", line 123, in <module>
    app = aFrame(root)
  File "main_GUI.py", line 23, in __init__
    self.initUI()
  File "main_GUI.py", line 44, in initUI
    imageLeft = Image.open("resource/empty.png")
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2634, in open
    fp = builtins.open(filename, "rb")
IOError: [Errno 2] **No such file or directory: 'resource/empty.png'**

error

hi im getting error all

quadratic speed up

So the loop in ImageObject.analyze

        for i in tqdm(range(featureContainerLength)):
            for j in range(i + 1, featureContainerLength):

can be changed to

        for i in tqdm(range(featureContainerLength-1)):
            j = i+1

giving a speedup of a factor >1000 on the analyze step (depending on image dimensions).
That step on the test image goes from 384 to 0.11 seconds on my PC.

This can be done because the feature list is already sorted, so you don't have to compare each block to every other block. You only need to compare it to the next block.

The original paper clearly states this:

identical blocks correspond to adjacent pairs in the sorted list

Error when computing c4_part1

Hi - I am trying to run the code over the horse_fake.png example, but I have reduced the blocksize to decrease the runtime, with the following problem on macOS:

Traceback (most recent call last):
File "main.py", line 10, in
forgery_detect.detect('../test_images/', image_file, '../output_images/', blockSize=8) # was 32
File "/Users/j/src/Image-Forgery-Detection/forgery_detect/forgery_detect.py", line 22, in detect
imageResultPath = singleImage.run()
File "/Users/j/src/Image-Forgery-Detection/forgery_detect/image_object.py", line 82, in run
self.compute()
File "/Users/j/src/Image-Forgery-Detection/forgery_detect/image_object.py", line 127, in compute
self.featuresContainer.addBlock(imageBlock.computeBlock())
File "/Users/j/src/Image-Forgery-Detection/forgery_detect/Blocks.py", line 38, in computeBlock
blockDataList.append(self.computeCharaFeatures(4))
File "/Users/j/src/Image-Forgery-Detection/forgery_detect/Blocks.py", line 118, in computeCharaFeatures
c4_part1 += self.imageGrayscalePixels[xCoordinate, yCoordinate]
TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'

Any ideas?

Thanks!

Not able to fetch and get the output

ERROR SHOWN-

C:\Users\alman\anaconda3\python.exe E:/image-copy-move-detection-python2-master/main_GUI.py
E:/image-copy-move-detection-python2-master/screenshot/horse_blur.png
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\alman\anaconda3\lib\tkinter_init_.py", line 1892, in call
return self.func(*args)
File "E:\image-copy-move-detection-python2-master\main_GUI.py", line 91, in onFilePicker
imageRight = Image.open("../testcase_result/")
File "C:\Users\alman\anaconda3\lib\site-packages\PIL\Image.py", line 2975, in open
fp = builtins.open(filename, "rb")
PermissionError: [Errno 13] Permission denied: '../testcase_result/'
horse_blur.png
Step 1 of 4: Object and variable initialization
Exception in Tkinter callback
<ImageObject.ImageObject object at 0x000001A22EC54B80>
Traceback (most recent call last):
File "C:\Users\alman\anaconda3\lib\tkinter_init_.py", line 1892, in call
return self.func(*args)
File "E:\image-copy-move-detection-python2-master\main_GUI.py", line 109, in onDetect
imageResultPath = CopyMoveDetection.detect(self.imagePath, self.imageName, '../testcase_result/', blockSize=32)
File "E:\image-copy-move-detection-python2-master\CopyMoveDetection.py", line 56, in detect
singleImage = ImageObject.ImageObject(sourceDirectory, fileName, blockSize, outputDirectory)
File "E:\image-copy-move-detection-python2-master\ImageObject.py", line 71, in init
print (self).Nb, self.isThisRGBImage
AttributeError: 'NoneType' object has no attribute 'Nb'

Confusion Matrix

This would be the last help i would ask from you. Thank you so much for all the help you have done. I tried different codes for confusion matrix and none of them worked for me. I just need the code for confusion matrix .So could you please help me with this. And i would be really greatfull for all your help.

Implementation of GUI for Py 3.x

Im trying to implement the GUI for the new repo with the help of your old repo, but im getting an error. Below is the error what im getting now. can you please help me fix it. And ill attach the GUI code and the detect.py code to this.

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\shuch\AppData\Local\Programs\Python\Python38\lib\tkinter_init_.py", line 1883, in call
return self.func(*args)
File "D:/image-copy-move-detection-master/copy_move_detection/Main1.py", line 96, in onDetect
imageResultPath = detect.detect(self.imagePath, self.imageName, '../output/', block_size=32)
TypeError: detect() got multiple values for argument 'block_size'

copy_move_detection.zip

Example Code Not Working

The current example code in the readme (directories, filename, ...) does not match detect.py input. Would be nice to have a command line "python3 detect image.jpg" example.

Error on use

Installing and using pimage like so:

from pimage import copy_move

copy_move.detect("dataset_example_blur.png", "output", verbose=True)

Will result in error:

image

Accuracy

Do you have any code to find the accuracy ?. It would be helpful if you provide any code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.