@@ -0,0 +1,2005 @@
+'''%0AThis is a demo that how to use Mosaic Model to detect image.%0AUsage:  python mosaic_porngraphic_cnn_demo.py the_path_of_image%0AOutput: The PORNGRAPHIC probability%0A%0AAuthor: boozyguo%0Amail: 44167841@qq.com%0Aupdate: 2016-11-04%0A'''%0A%0A# model reconstruction from JSON:%0Afrom keras.models import model_from_json%0Afrom keras.preprocessing.image import array_to_img, img_to_array, load_img%0Aimport h5py%0Aimport os%0Aimport argparse%0A%0Aprint ('%5Cr%5CnMosaic: detect porngraghic')%0Aprint ('=====================================')%0A%0Aparser = argparse.ArgumentParser(description='Detect Porngraphic Images by Deep Learning with Keras.')%0Aparser.add_argument('input_image_path', metavar='base', type=str,%0A                    help='Path to the image to detect.')%0A%0Aargs = parser.parse_args()%0Ainput_image_path = args.input_image_path%0A%0A%0A# input image dimensions%0Aimg_rows, img_cols = 32, 32%0Aimg_channels = 3%0A%0A# load model%0Aif os.path.exists('./model/mosaic_porngraphic_cnn_architecture_release.json'):%0A    model = model_from_json(open('./model/mosaic_porngraphic_cnn_architecture_release.json').read())%0A    print ('loading CNN model..........')%0A    print ('load OK!')%0Aelse:%0A    print ('Not Find Model........')%0A    exit()%0A%0A# load weights%0Aif os.path.exists('./model/mosaic_porngraphic_cnn.release.hdf5'):%0A    model.load_weights('./model/mosaic_porngraphic_cnn.release.hdf5')%0A    print ('loading weights..........')%0A    print ('load OK!')%0Aelse:%0A    print ('Not Find Weights........')%0A    exit()%0A%0A# try to predicts some images%0Aprint ('%5Cr%5CnTry to Predict input images:')%0Aprint ('=====================================')%0Are_img = load_img(input_image_path)  # this is a jpg image%0Are_img = re_img.resize((img_cols,img_rows))%0Ax_img = img_to_array(re_img)  # this is a Numpy array with shape (3, 32, 32)%0Ax_img = x_img.reshape((1,) + x_img.shape)  # this is a Numpy array with shape (1, 3, 32, 32)%0Aprint ('Input image is: %25s' %25input_image_path)%0Aprint ('The PORNGRAPHIC probability is: %253.3f%25%25 %5Cr%5Cn' %25(100*model.predict_on_batch(x_img)))%0A%0A%0A%0A%0Aexit()%0A%0A

test commit